Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Macro-'Expected end of statement' Error

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.

8 Replies
el_aprendiz111
Specialist
Specialist

Hi

Macro Qlik:

SUB fAdd()
x=10
y=25
z=x+y
MSGBOX z
end SUB

Anonymous
Not applicable
Author

No need to declare the variables here?

el_aprendiz111
Specialist
Specialist

nop

Anonymous
Not applicable
Author

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

Anonymous
Not applicable
Author

so in this case variable needs to be declared first

el_aprendiz111
Specialist
Specialist

Hi

sub fAdd2()

x=inputbox("enter x value")

y=inputbox("enter y value")

z=int(x)+ int(y)

msgbox z

end sub

Anonymous
Not applicable
Author

Thank you @fer fer

so basically, here we don't use dim to declare a variable.right?

marcus_sommer

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