Home very basic VBA function not working
Reply: 2

very basic VBA function not working

Thimo Demey
1#
Thimo Demey Published in 2018-01-12 14:56:47Z

At first, I wanted to get myself some knowledge about VBA because it's used often at the company I recently started working. So I decided to do a tutorial about it. I have some knowledge about vb.net from when I went to school.

I do know this is very basic and there is a built-in 'SUM function' in Excel.

While I'm trying to select the 2 cells with the numbers in does this error appear when I select the 2nd cell: (English version error message from the internet)

The code I typed for the function:

Function fnSum(Num1 As Double, Optional num2 As Variant)
    If IsMissing(num2) Then
        fnSum = Num1 * Num1
    Else
        fnSum = Num1 * num2
    End If
End Function
Foxfire And Burns And Burns
2#
Foxfire And Burns And Burns Reply to 2018-01-12 15:04:39Z

Try this one

Function fnSum(ByRef Num1 As Double, Optional ByRef num2 As Variant)
    If num2 = "" Then
        fnSum = Num1 * Num1
    Else
        fnSum = Num1 * num2
    End If
End Function

Worked for me perfectly

Mohammad ElNesr
3#
Mohammad ElNesr Reply to 2018-01-12 15:15:21Z

You have to put the default value of the optional parameter

Sub fnSum(ByRef Num1 As Double, Optional ByVal num2 As variant = "")
    If num2 = "" Then
        fnSum = Num1 * Num1
    Else
        fnSum = Num1 * num2
    End If
End Function
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.326626 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO