Home > Net >  VBA ByRef argument type mismatch even though types match?
VBA ByRef argument type mismatch even though types match?

Time:01-25

I have a String() array, which I am looping through using For Each. Inside of this loop, I am running a function that takes the String as an argument. I keep getting ByRef argument type mismatch when calling the function, and I'm not sure why. I can confirm that the variables used are strings:

enter image description here

though one of them is an empty string, that shouldn't matter. Any ideas?

enter image description here

PeriodArray = f.getPerArray
PreviousPeriodSheet = ""
For Each PeriodSheet In PeriodArray
    Debug.Print PeriodSheet & " " & TypeName(PeriodSheet) & " | " _
              & PreviousPeriodSheet & " " & TypeName(PreviousPeriodSheet)
    
    Call RenderPeriod(PeriodSheet, PreviousPeriodSheet)
    PreviousPeriodSheet = PeriodSheet
Next

Here's the parameters for the RenderPeriod function

Function RenderPeriod(PeriodName As String, PrevPeriodName As String)

And here's the f.getPerArray function, which is where the String() array comes from:

Function getPerArray() As String()

I'm no VBA wiz, but I don't see why it's not accepting my Strings.

CodePudding user response:

PeriodSheet is either undeclared, or declared as a Variant. You haven't declared it as String or you'd be getting the error "For Each control variable on arrays must be Variant" from the For Each line.

So if PeriodSheet needs to be a Variant you can avoid the type mismatch error when calling the function by doing something like this:

RenderPeriod CStr(PeriodName), PreviousPeriodSheet

Note PreviousPeriodSheet should be declared As String

  • Related