Parsing variables between subroutines

stonysleep

New Member
Joined
Feb 2, 2011
Messages
10
I'm doing something which is probably foolish - namely taking some vba that i've written that works and trying to make it more technically correct by using subroutines.
It doesn't need it as it is fast enough as it is but i'm a perfectionist and if it ain't broke don't fix it is about to become if it ain't broke it will be after i've messed about with it!

Anyway, i'm not very good with subroutines and coming a cropper when it comes to parsing variables from 1 sub into another.

What I am trying to do is call a sub routine from another and parse 3 variables but i keep getting an error message:
"Compile error ByRef argument type mismatch"

This works:
Code:
sub test()
Call drill("a", "b", "c")
end sub
 
Sub drill(ByVal job As String, flagname As String, sheetname As String)
Debug.Print job & " " & name & " " & sheetname
...lots of code
end sub

This doesn't:
Code:
Sub test()
flag = "a"
flagref = "b"
newname = "c"
Call drill(flag, flagref, newname)
End Sub
 
Sub drill(ByVal job As String, flagname As String, sheetname As String)
Debug.Print job & " " & name & " " & sheetname
...lots of code
end sub

In actual fact the variables flag, flagref and newname are cell references but since it doesn't even work when setting the variables to a, b and c i must be doing something fundamentally wrong.
I tried changing ByVal in the called procedure to ByRef but that didn't change anything - same error message

Any help much appreciated
Thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This works:

Code:
Sub test()
    flag = "a"
    flagref = "b"
    newname = "c"
    Call drill(flag, flagref, newname)
End Sub

Sub drill(job, flagname, sheetname)
    Debug.Print job & " " & flagname & " " & sheetname
End Sub
 
Upvote 0
If you put Option Explicit at the top of each module, the compiler will tell you that you have undefined variables.

If in the VBE, you do Tools > Options > Editor, and tick Require variable declaration, that line will automatically appear at the top of each new module.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top