ByRef Error

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 this line of code calling a procedure.

Rich (BB code):
create_proglist fac, facPgrmTar, timeOpen, timeClose, cdcnt

In my procedure, timeOpen and timeClose (time values) are declared as Date.
Code:
Dim timeOpen as date, timeClose as date

Here is the procedure sub being called:
Code:
Sub create_proglist(ByRef fac As String, facPgrmTar As Range, timeOpen As Date, timeClose As Date, cdcnt As Double)

Can someone help by suggesting what I need to do to correct this?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Put a break point somewhere (after the point where all variables have been assigned values or at the beginning of create_proglist). When the code is still stopped, examine the variables to see what they contain. Can mouse over them or use immediate window of vb editor, as in typing:
?fac and hit enter. Value should appear below. If you get a blank line, it's null or a zls (zero length string). The data type must match what's expected in ByRef parameters but I'm not certain that zls or null would be allowed. If you get a value that looks right, can test its data type with:

?typename(fac) and hit enter. Should return "string"
Note that a number can be string or numeric data, so watch for that. You might be allowing "5" to be passed as a number when you think it is a string.
 
Upvote 0
Hi,
If you don’t want to modify your original data in the calling sub but need to use that data in another sub, then you do not need to pass your arguments ByRef - just pass them ByVal

VBA Code:
Sub create_proglist(ByVal fac As String, ByVal facPgrmTar As Range, ByVal timeOpen As Date, ByVal timeClose As Date, ByVal cdcnt As Double)

End Sub

Dave
 
Upvote 0
put a break point somewhere (after the point where all variables have been assigned values or at the beginning of create_proglist). When the code is still stopped, examine the variables to see what they contain. Can mouse over them or use immediate window of vb editor, as in typing:
?fac and hit enter. Value should appear below. If you get a blank line, it's null or a zls (zero length string). The data type must match what's expected in ByRef parameters but I'm not certain that zls or null would be allowed. If you get a value that looks right, can test its data type with:

?typename(fac) and hit enter. Should return "string"
Note that a number can be string or numeric data, so watch for that. You might be allowing "5" to be passed as a number when you think it is a string.
Thanks Micron for your suggestion, but I can't get that far. It breaks with an error when I compile the code (via Debug), or if I step through the procedure row by row. As soon as I press F8 on the 1st line of code for the procedure it errs. I can't get to the point where values are assigned to the variables. (timeOpen etc)


If you don’t want to modify your original data in the calling sub but need to use that data in another sub, then you do not need to pass your arguments ByRef - just pass them ByVal
The value for timeOpen and/or timeClose could change as a result of the code being called. Even when I change from ByRef to ByVal I get the same error.
 
Upvote 0
I found my error. My declaration statement was incomplete.

Code:
Dim timeOpen As Date, timeClose As Date
is correct. My original line,
Code:
Dim timeOpen ,timeClose As Date
is obviously incorrect.
 
Upvote 0
Solution
I found my error. My declaration statement was incomplete.

Code:
Dim timeOpen As Date, timeClose As Date
is correct. My original line,
Code:
Dim timeOpen ,timeClose As Date
is obviously incorrect.
Yeah, I looked for an incorrect declaration but what you said you had looked ok - except it's apparently not what you had. ;)
Because it wasn't specifically typed, you were using a variant, not a string. When using ByRef, data types must be the same AFAIK.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,042
Members
448,940
Latest member
mdusw

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