I have some pretty simple code where I am trying to pass an argument from one sub (call parent) to another; modify some values, not modify some others and finish. i seem to fail miserably. not sure why. Here is some code
Sub Parent_sub()
Dim v1,v2,v3 as Integer
v1 = 0
v2 = -1
v3 = -2
Child_sub v1, v2, v3
msgbox "the variables are " & v1 & " ," & v2 & ", and " & v3
End Sub
Sub Child_sub(ByVal c1 as Integer, ByRef c2 as Integer, ByRef c3 as Integer)
c1 = 3
c2 = 4
c3 = 5
End Sub
I struggle to see why I get a compilation error (Compile error; ByRef Argument Type Mismatch; it appears at the Child_sub call, on the v2 argument). I use Excel 2016 for Home and Business. Any helpful tips?
Just to save from some potentially simple responses; I have not defined two subs with the same name (!), neither are the variables further redefined anywhere else in the code (checked). its quite a bizzare one for me. Please help, I'm an idiot!
VBA doesn't have semicolons so you need to remove those. That is actually the only syntax error in your code. The others are just functionality.
Dim v1,v2,v3 as Integer
only makes v3 an Integer. You need to repeat the data type for every variable.
Finally, if you want the child sub to edit the variable you need the ByRef
keyword. You used it on c2 and c3 but not on c1
Sub Parent_sub()
Dim v1 As Integer,v2 As Integer,v3 as Integer
v1 = 0
v2 = -1
v3 = -2
Child_sub v1, v2, v3
msgbox "the variables are " & v1 & " ," & v2 & ", and " & v3
End Sub
Sub Child_sub(ByRef c1 as Integer, ByRef c2 as Integer, ByRef c3 as
Integer)
c1 = 3
c2 = 4
c3 = 5
End Sub