简体   繁体   中英

Match function in VBA gives compile error

I have a work book with two Worksheets. '2020-2021' Worksheet has some unique numbers in its Column D. Result of Match should come to 'Arrears' Worksheet. C2 cell in 'Arrears' has a number which I want to Match in Column D in 2020-2021. Entered the following code.

Range("C3").Value = WorksheetFunction.Match(Range("c2").value,Range('2020-2021'!d11:d206),0)

Gives Compile Error

Qualify (Use Variables)

  • If you feel like learning something, study the first two versions.
  • If not, pick one of the solutions in the third version.

Qualify; what does that even mean?

  • Related to this case, eg Range("C3") is not qualified. We don't know for sure where it refers to (belongs). It is short for ActiveSheet.Range("C3") which means that if we select Sheet1 it will refer to C3 on Sheet1 , if we select Sheet2 it will refer to C3 on Sheet2 etc.

  • But if we use Worksheets("Sheet1").Range("C3") we have qualified the range and which ever worksheet we select, it will always refer to C3 on worksheet Sheet1 (Not 100% true because...).

  • Similarly to the range case, we are saying that the worksheet in Worksheets("Sheet1").Range("C3") is not qualified. The expression is actually short for ActiveWorkbook.Worksheets("Sheet1").Range("C3") .

  • To qualify it, we can do Workbooks("Test.xlsm").Worksheets("Sheet1").Range("C3") . Now we are saying that the range is fully qualified, as is the worksheet. This might often seem unpractical, so ThisWorkbook 'comes to the rescue'.

  • If you need to refer to the workbook containing this code, just use ThisWorkbook . You don't care if it is called Test.xlsm or Last Years Official Inventory Report whatever.xlsm ... just use ThisWorkbook .

  • Related to our case, we can then fully qualify our range using:

     ThisWorkbook.Worksheets("Arrears").Range("C3")

Application.Match vs WorksheetFunction.Match

Sub qualifyAP()
    
    ' Define workbook.
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    ' Define worksheets.
    Dim src As Worksheet
    Set src = wb.Worksheets("2020-2021")
    Dim tgt As Worksheet
    Set tgt = wb.Worksheets("Arrears")
    
    ' Use the Application version of Match.
    Dim Result As Variant ' can hold any datatype incl. error value.
    Result = Application.Match(tgt.Range("C2").Value, src.Range("D11:D206"), 0)
    If Not IsError(Result) Then
        tgt.Range("C3").Value = Result
        'MsgBox "Data found and transferred."
    Else
        MsgBox "Data not found. Nothing done."
    End If

End Sub

Sub qualifyWF()
    
    ' Define workbook.
    Dim wb As Workbook
    Set wb = ThisWorkbook ' The workbook containing this code.
    
    ' Define worksheets.
    Dim src As Worksheet
    Set src = wb.Worksheets("2020-2021")
    Dim tgt As Worksheet
    Set tgt = wb.Worksheets("Arrears")
    
    ' Use the WorksheetFunction version of Match.
    On Error Resume Next
    tgt.Range("C3").Value = WorksheetFunction.Match(tgt.Range("C2").Value, _
                                                    src.Range("D11:D206"), _
                                                    0)
    If Err.Number = 0 Then
        'MsgBox "Data found and transferred."
    Else
        MsgBox "Data not found. Nothing done."
    End If
    On Error GoTo 0

End Sub

Sub qualifyQF()
    
    Worksheets("Arrears").Range("C3").Value = WorksheetFunction _
      .Match(Worksheets("Arrears").Range("C2").Value, _
             Worksheets("2020-2021").Range("D11:D206"), _
             0)
' or:
    With Worksheets("Arrears")
        .Range("C3").Value = WorksheetFunction _
          .Match(.Range("C2").Value, _
                 Worksheets("2020-2021").Range("D11:D206"), _
                 0)
    End With
' or even:
    With Worksheets("Arrears").Range("C3")
        .Value = WorksheetFunction _
          .Match(.Offset(-1).Value, _
                 Worksheets("2020-2021").Range("D11:D206"), _
                 0)
    End With

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM