title: VBA ByRef Argument Type Mismatch When Using Function In If Statement
body: <p>I am having a weird issue when I'm calling a function from another function inside an <code>if</code> statement. I defined a <code>Sub</code>, which I am using to test this function, and it calls on a function which relies on another function I use to compare values. The code is below, which should make things clear. Essentially, I don't understand how it's possible for the code to work fine in the <code>print</code> statement, and then throw an error in the <code>GetMatch</code> function. I appreciate any help.</p>
  
  <p>Edit: All of a sudden everything works. Do debugging breakpoints affect the program? I haven't changed anything, but CStr() is no longer required when calling GetMatch. I haven't touched any of the subs or functions, but I did clear some breakpoints. If I find what caused it, I'll post a solution. Thanks for the help everyone.</p>
  
  <p>Edit2: Maybe this is a bug with VBA? If I add the CStr() option to the indexOrder(...) calls, things work. Before, without the CStr() options, things did not work. Now, strangely enough, after using the CStr(), I am able to remove the CStr()'s entirely from the program, and things work again. It breaks if I undo to the point where they weren't there originally though. I don't know what this could be, but if anyone has an explanation, I'm very interested. Thanks</p>
  
  <pre><code>Sub testFind()
      Dim SortOrder() As Variant
      Dim indexOrder() As Variant
      SortOrder = Array("Contact Email", "Last Name", "First Name", "Attempt #", "Customization", "Template #")
      indexOrder = Array("First Name", "First Name", "Template #", "Customization")
      findAndReplace(indexOrder, SortOrder)
  End Sub
  
  
  Function findAndReplace(indexOrder As Variant, list As Variant) As Variant
      Dim indexLength As Integer
      Dim listLength As Integer
      Debug.Print TypeName(indexOrder(0)) ' Identifies as String
      indexLength = getVariantLength(indexOrder)
      listLength = getVariantLength(list)
      Debug.Print GetMatch(CStr(indexOrder(1)), CStr(indexOrder(1))) ' This works fine. Returns 0 as it should
      If GetMatch(indexOrder(1), indexOrder(1)) = 0 Then ' Fails with ByRef error
          Debug.Print ("Why don't I work?")
      End If
  End Function
  
  
  Function GetMatch(A As String, B As String) As Integer
      A = Trim(A)
      B = Trim(B)
      If (IsEmpty(A) Or Trim(A) = "") Then
          GetMatch = 1
          Exit Function
      ElseIf (IsEmpty(B) Or Trim(B) = "") Then
          GetMatch = -1
          Exit Function
      End If
      GetMatch = StrComp(A, B, vbTextCompare)
  End Function
  
  Function getVariantLength(vari As Variant) As Integer
      If IsNull(index) Then
          getVariantLength = 0
      Else
          getVariantLength = UBound(vari) - LBound(vari) + 1
      End If
  End Function
  </code></pre>