简体   繁体   中英

Excel VBA: How to return a partial match

I've looked through the questions and can't seem to find an answer.

I have a list of receipt IDs in a spreadsheet, created by system 1. In another spreadsheet, I also have a list of the same receipts, created by system 2.

90+% of the information is presented the same, and I can run a Match using worksheetfunction.match. However, there is a particular type of receipt (a refund and purchase) that has a different format in system 2.

An example would be:

System 1:

  • AAAA0000001
  • AAAA0000002
  • AAAA0000003
  • AAAA0000004
  • ....
  • ZZZZ9999999

System 2:

  • AAAA0000001
  • AAAA0000002
  • AAAA0000003 / AAAA000004 - this would be a refund and purchase
  • AAAA0000005
  • ....
  • ZZZZ9999999

The code I am using is:

While MC > MD

    Worksheets("ID sheet").Activate                             ' activate ID sheet

    MF = WorksheetFunction.VLookup(MD, Range("U2:W" & Range("U1")), 2, False)   ' Vlookup for System 1 letters
    MG = WorksheetFunction.VLookup(MD, Range("U2:W" & Range("U1")), 3, False)   ' Vlookup for System 1 numbers
    MH = MF & MG                                                ' combine GFAS and Fund IDs

     Worksheets("System 1").Activate                ' Activate System 1 screen
        MI = WorksheetFunction.Match(MH, Range("HC:HC"), 0)         ' Match combined letters and numbers for System 1
        MK = Range("A" & MI)                                        ' Find deal ID

        Worksheets("System 2").Activate                         ' Activate System 2 screen
        ML = WorksheetFunction.Match(MK, Range("W:W"), 0)           ' Match deal ID in Range W
        MM = Range("F" & ML)                                        ' Find Register ID

        MD = MD + 1

    Wend

The code returns a run time error 1004.

Is there any way that I can get the code to accept the double entry as a match and return an answer?

Any help is gratefully appreciated.

如果我了解您要匹配的内容,则可以使用:

MI = WorksheetFunction.Match("*" & MH & "*", Range("HC:HC"), 0)

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