"ByRef argument type mismatch" While Passing Variables Between Modules

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Thank you all for your insights. Not quite sure what the solution was, I was a bit side tracked when the discussion kinda lost focus (to me). I simply ended up making all the variables public. I've been afraid to depend on doing that as I'm afraid that having too many public variables might cause longer term problems? Is this true? Can having too many publicly declared variables cause issues?
 
Upvote 0
Not quite sure what the solution was,
That was post 6.
In layman's terms, your second procedure was expecting 10 arguments to be passed, you were only passing 3.

Regarding public variables, I've not had any problems with them personally, but I've encountered other users that have, and as I mentioned in an earlier reply, I'm reluctant to advise them on the forum based on the problems that others have encountered.

One thing to remember is that public variables are static, they hold value when the procedure ends. If not used correctly you can end up with errors caused by stale variables that have not been emptied or updated.
 
Upvote 0
That was post 6.
In layman's terms, your second procedure was expecting 10 arguments to be passed, you were only passing 3.

Regarding public variables, I've not had any problems with them personally, but I've encountered other users that have, and as I mentioned in an earlier reply, I'm reluctant to advise them on the forum based on the problems that others have encountered.

One thing to remember is that public variables are static, they hold value when the procedure ends. If not used correctly you can end up with errors caused by stale variables that have not been emptied or updated.
Thanks jasonb, my understanding of passing variables is clearly flawed then. I feel as a result I'm relying on publiv variables where simply passing the variables, if done right, is more than adequate.
 
Upvote 0
my understanding of passing variables is clearly flawed then.
So was mine, hence the discussion between @Fluff, @mikerickson and myself.

Have a look at this link to see if it helps with your understanding. This was something that I was aware of but for some reason I thought that it only applied to functions, not to procedures (subs).
edit:- Second link with a bit more info
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
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