Calling Function

  • Thread starter Thread starter mattsvai
  • Start date Start date
M

mattsvai

How can I call a custom function without VBA asking me to assign it to
variable?

Ex:

Sub test()

customFunction(arg1,arg2)

end Sub

is there any way to do this without msgboxing it or assigning it?

Cheer
 
Remove "Option Explicit" at the top of the module

The general syntax of a variable declaration is:
Dim VariableName As DataType
If a particular variable is used without first declaring it, or if it
is declared without mentioning a
data type, as in:
Dim Age
then VBA will treat the variable as having type Variant. this is
generally a waste of memory, since variants require more memory than
most other types of
variables.

I always decler variable, then you now wich data type it is.

Regard Yngve
 
customFunction(arg1,arg2)

Hi. With your use of ( ), the function is expecting to return a value. Try
it without the use of ( ).

Sub TestIt()
Dim arg1, arg2

CustomFunction arg1, arg2
'or..
Call CustomFunction(arg1, arg2)
End Sub

Function CustomFunction(x, y)
CustomFunction = x * y
End Function
 
I entered the following:

Sub TestIt()
arg1 = Range("b6").Value
arg2 = Range("B10").Value
CustomFunction arg1, arg2
MsgBox "The Answer is " & CustomFunction << But I get Compile error here!!
Why?
End Sub

Function CustomFunction(x, y)
CustomFunction = x * y
End Function
 
Never mind,,, Figured it Out!!
Corrected as follows:

Sub TestIt()
arg1 = Range("b6").Value
arg2 = Range("B10").Value
MyAnswer = CustomFunction(arg1, arg2)
MsgBox "The Answer is " & MyAnswer
End Sub

Function CustomFunction(x, y)
CustomFunction = x * y
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top