Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
I am new to VBA and trying to write below code in module.
function fAdd()
Dim x as Integer
Dim y as Integer
fAdd=x+y
end function
But getting an error saying 'Expected end of statement' with as Integer being highlighted at the second line.
Plz help.
Thanks in Advance.
Hi
Macro Qlik:
SUB fAdd()
x=10
y=25
z=x+y
MSGBOX z
end SUB
No need to declare the variables here?
nop
but when you take value from user, the variable is taking string value only
sub fAdd()
x=inputbox("enter x value")
y=inputbox("enter y value")
msgbox x+y
end sub
say x=10 and y=15
Expected o/p should be 25
but actual o/p is 1015
so in this case variable needs to be declared first
Hi
sub fAdd2()
x=inputbox("enter x value")
y=inputbox("enter y value")
z=int(x)+ int(y)
msgbox z
end sub
Thank you @fer fer
so basically, here we don't use dim to declare a variable.right?
Best practice would be of course to declare the variables and also to use the option explicit within the very first line - but what you don't need within vbs is to specify the datatype of a variable like: Dim x as Integer - it will cause here an error.
Do you the same within vba you should declare the datatype of a variable to save resources because without declaring each variable will be handled as object from a RAM point of view. Further helpful by larger scripts or projects is to use prefixes to the variable-name to tag the wanted/needed datatype like:
sString = "s is the prefix for a string"
iNumber = 2016 ' i = integer
- Marcus