"ByRef argument type mismatch" While Passing Variables Between Modules

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am receiving a "ByRef argument type mismatch" error with the piece highlight in red in the code below. The Variable st was declared in the module that called the inf_prep routine.
Rich (BB code):
Sub inf_prep (srow As Integer, pnum As String, fac2 As String, nrec As Long, st As Variant, pt As String, bkg_date As Date, prep_type As String, prep_date As Date, prep_time As String, posnum As Long, posx As Long)

    Dim d_cell As Range 'destination cell on ws_master to send crew
    Dim bkg_date As Long

    Set d_cell = ws_master.Cells(srow, 9)
    bkg_date = CLng(ws_master.Range("M1").Value)

    If pt = "D" Or pt = "F" Or pt = "C" Then
        stop
    Else
        prep_active st, fac2, bkg_date
    End If
    '  .  .  .  .  .  more code >
End Sub

Rich (BB code):
Sub prep_active(srow As Integer, st As Variant, pnum As String, fac2 As String, bkg_date As Long, prep_type As String, prep_date As Date, preptime As String, posnum As Long, posx As Long)
    Dim srow As Integer
    Dim st As Variant
    Dim pnum As String
    Dim fac2 As String
    Dim cnt_b_label As Long, posnum As Long, posx As Long
    Dim prep_type As String
    Dim prep_date As Date
    Dim prep_time As String
    Dim d_th As Long
    Dim sr As Range
  
    '  .  .  .  . < more code >  .  .  .  .

            prep_type = "CHK" 'checkup
            prep_date = Format(bkg_date, "dd-mmm")
            prep_time = "<" & Format(st - TimeSerial(0, 30, 0), "h:MM A/P")
      
    '  .  .  .  . < more code >  .  .  .  .
End Sub
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
The variable st is defined as variant, but you are passing it to the next sub, where it's expecting an Integer.
 
Upvote 0
st is data type Variant, it is being passed as the sRow argument, which is data type Integer.
You need to explicitly convert it to an integer

VBA Code:
prep_active CInt(st), fac2, bkg_date
 
Upvote 0
Would I be best to define "st" as an integer instead of a variant? Would that resolve the error without mikerickson's suggestion (only because of my lack of understanding and the liklihood of it happening elsewhere again)?
I don't really understand why st is taking the srow argument as they are separate, both brough in from the calling procedure, although srow isn't being used in this module's code. Yet.
 
Last edited:
Upvote 0
This is not the method that I would use personally but comparing the 2 procedures, I'm thinking that it should be one of the following (not sure that my syntax is correct, @Fluff / @mikerickson could you correct as necessary please).

As I understand it st is being passed to the srow argument of the second procedure, not the st argument, which is what it looks like you want.
VBA Code:
prep_active , st, fac2, bkg_date
VBA Code:
prep_active st:=st, fac2:= fac2,bkg_date:= bkg_date
Using the same variable names in both procedure could end up as a conflict though :unsure:
 
Upvote 0
None of the arguments in your prep_active are optional, so you need to pass all of them.
 
Upvote 0
That may be correct but it appears logically flawed (in my opinion)
Take the following as an example
VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel is not declared as optional and given that the action is only cancelled when Cancel = True is used with the procedure suggests that any variable not passed will use the default value for the type unless it is changed during the course of the procedure.
 
Upvote 0
I would suspect that Xl is passing False to the Event for the Cancel argument, but know for sure.
 
Upvote 0
That may be correct but it appears logically flawed (in my opinion)
Take the following as an example
VBA Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel is not declared as optional and given that the action is only cancelled when Cancel = True is used with the procedure suggests that any variable not passed will use the default value for the type unless it is changed during the course of the procedure.
Just because the Cancel argument isn't used is not evidence that it is optional.
If you call the Worksheet_BeforeRightClick event yourself, you will need to pass a Boolean as the second argument.
 
Upvote 0
Just because the Cancel argument isn't used is not evidence that it is optional.
I wasn't trying to suggest that it was, Mike, only how it appeared to me.
It's not a theory that I've ever had cause to test or even given thought to until I looked at this thread, which was why I asked for either Fluff or yourself to correct any bad advice that I may have given.

I've used optional arguments with an accompanying default value for functions but for procedures I prefer to avoid arguments and use public variables instead, although that is something that I avoid suggesting on the forum after the last attempt resulted in a seemingly never ending thread.
 
Upvote 0

Forum statistics

Threads
1,215,223
Messages
6,123,715
Members
449,118
Latest member
MichealRed

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