Partial Matching between two sheets

zios007

New Member
Joined
Jul 14, 2022
Messages
14
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Everyone! I'm new here and new to VBA. I'm trying to automate few things but I've spent lots of hours already looking on the internet, videos, forums, etc... and haven't been able to achieve anything, no success.

Here is what I'm trying to achieve, let see if I know how to explain it clearly.

- I have sheet1 with a list of descriptions
- I have sheet2 with a list of references
- The references in sheet2 can be found in the sheet1 list of descriptions but it is not an exact match since the descriptions have a lot of gibberish text in front and in the back of the reference.
- I need to do a partial match check from the reference in sheet2 with sheet1
- If the reference from sheet2 exists in any of the sheet1 descriptions (if it matches), then I would like to return columns b, c and d from sheet2 to columns in sheet1 f, h and i.
- Note: There may be times in sheet2 that one of the columns may not have data since it will get provided later. So if no data I would like to leave a blank field in sheet1.

I've been trying to test only to return c from sheet2 to h in sheet1 to start with, but not success.

These are my 2 sheets examples:

Sheet1:
Sheet1.PNG


Sheet2:
Sheet2.PNG


And here is a piece of code that I've been trying, but not results appear anywhere. If the code is crap, please change it as you prefer. I'm already too lost... maybe it is easier coding it differently.

VBA Code:
Sub Find_ISL_REF()

 Dim rng2 As Range, c2 As Range, cfind As Range
    Dim x, y
    With Worksheets("Sheet1")
        Set rng2 = .Range(.Range("C2"), .Range("C2").End(xlDown))
        For Each c2 In rng2
            x = c2.Value
            With Worksheets("Sheet2").Columns("A:A")
                On Error Resume Next
                Set cfind = .Cells.Find(what:=x, lookat:=xlPart, LookIn:=xlValues)
                If (Not (cfind Is Nothing)) Then
                    y = cfind.Offset(0, 5).Value
                    c2.Offset(0, 5) = y
                End If
            End With
        Next c2
    End With

End Sub

I hope one of you brilliant and intelligent members can help me out. I'm sure it is much easier that I've been trying to code it for. But at this point, after hours of researching and testing, I come here for help.

Thank you very much in advance. I will appreciate any help.

Bests,
ZioS
 
Do you want the formulas to appear in the workbook or just the result ?
Can you give me an XL2BB for your chart of accounts ?
If you want to put the workbook on a shared service such as dropbox, google drive ..... and allow anyone with the link access and post the link here that would probably make it easier.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Do you want the formulas to appear in the workbook or just the result ?
Can you give me an XL2BB for your chart of accounts ?
If you want to put the workbook on a shared service such as dropbox, google drive ..... and allow anyone with the link access and post the link here that would probably make it easier.
Just the results. I do not want formulas on the sheets. I want to run everything (or as much as possible) from a macro.

I will upload the file to a share service. I think it will make it easier. Will have to do it this afternoon, in few hours.
 
Upvote 0
Give this a try. It is using the table names and formulas you provided.
I can't find the Product number in any of the mapping tables, so that column is coming up empty/

VBA Code:
Sub GetAcctCoding()
    Dim shtTran As Worksheet
    Dim lrowTran As Long
    Dim rngTran As Range
   
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
   

    Set shtTran = Worksheets("Sheet1")                          '<--- Change the "Sheet1" to your transaction sheet name
    lrowTran = shtTran.Cells(Rows.Count, "A").End(xlUp).Row
    Set rngTran = shtTran.Range("A2:I" & lrowTran)
   
    rngTran.Columns(6).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet2Ref[Ref],'sheet1'!C2),Tablesheet2Ref[Account]),"""")"
    rngTran.Columns(8).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet2Ref[Ref],'sheet1'!C2),Tablesheet2Ref[Project]),"""")"
    rngTran.Columns(5).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet3DeptID[Project],'sheet1'!H2),Tablesheet3DeptID[Dept]),"""")"
    rngTran.Columns(7).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet4Accounts[Description Chart of Accounts],'sheet1'!G2),Tablesheet4Accounts[Description]),"""")"
    rngTran.Columns(9).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet4Accounts[Account],'sheet1'!F2),Tablesheet4Accounts[Description]),"""")"

    rngTran.Columns(5).Resize(, 5).Value = rngTran.Columns(5).Resize(, 5).Value
    rngTran.Columns(5).Resize(, 5).Replace What:="0", Replacement:="", LookAt:=xlWhole, _
            SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Dear Alex,

I've tested your piece of code and it looks like it is partially working!

It breaks when it gets to Columns(5)

Below is a Mega folder where I put a short version of the data set. Note that table names may be different from your code above. I've updated your code with the table names on the file.

It is almost working! :)

I've also left a tab named "sheet1 (2)" which is where the formulas are hard coded in each cell and it seemed to work.

Another request if possible: For sheet1, column I (the Description column), is if it possible to read (I guess with a AND/OR statement) for both Columns A and or B from the Accounts sheet?
The reason is because in the sheet1 column F I would like to be able to choose with a drop-down list but also have the ability to type the account (just the 4 digits) which will not have the description. But with my formula I was able to read only column A or B from the Accounts sheet, one or the other, but not both to find out if one of the two is true.

Drop-down list example sheet1, column F:
1658417888028.png


Sheet1, Column F example of both, a chosen account from the drop-down list and another line with a manual written account just with the 4 digits.
1658418225689.png


Note: the drop-down list hasn't been added yet.

Mega Folder

Thank you! I've been learning so much excel these past days... I still need to read your piece of code and see if I can understand what you did, cause it is like magic to me right now.
 
Upvote 0
That helped. A couple of your tablenames had changed.

Try this:

VBA Code:
Sub GetAcctCoding()

    Dim shtTran As Worksheet
    Dim lrowTran As Long
    Dim rngTran As Range
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    

    Set shtTran = Worksheets("Sheet1")                          '<--- Change the "Sheet1" to your transaction sheet name
    lrowTran = shtTran.Cells(Rows.Count, "A").End(xlUp).Row
    Set rngTran = shtTran.Range("A2:I" & lrowTran)
    
    rngTran.Columns(6).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet2Ref[Ref],C2),Tablesheet2Ref[Account]),"""")"
    rngTran.Columns(8).Formula = "=IFNA(LOOKUP(2,1/SEARCH(Tablesheet2Ref[Ref],C2),Tablesheet2Ref[Project]),"""")"
    rngTran.Columns(5).Formula = "=IFNA(LOOKUP(2,1/SEARCH(TableYorS[Project],H2),TableYorS[DeptID]),"""")"
    rngTran.Columns(7).Formula = "=IFNA(LOOKUP(2,1/SEARCH(TableAccounts[Description Chart of Accounts],G2),TableAccounts[Description]),"""")"
    rngTran.Columns(9).Formula = "=IFNA(LOOKUP(2,1/SEARCH(TableAccounts[Description Chart of Accounts],F2),TableAccounts[Description]),"""")"

    rngTran.Columns(5).Resize(, 5).Value = rngTran.Columns(5).Resize(, 5).Value
    rngTran.Columns(5).Resize(, 5).Replace What:="0", Replacement:="", LookAt:=xlWhole, _
            SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
PS: If you interested in looking into Power Query, I posted this link in response to another question on the forum today.

This is an 18 min video from Wyn Hopkins which deals with using partial matching to map / categorise the information in the manner that you are doing.

 
Upvote 0
PS: If you interested in looking into Power Query, I posted this link in response to another question on the forum today.

This is an 18 min video from Wyn Hopkins which deals with using partial matching to map / categorise the information in the manner that you are doing.

Definitely I will watch it. Thank you for sharing it.

The code works perfect!

Now I need to figure out how to do that and/or statement for this line of code
VBA Code:
rngTran.Columns(9).Formula = "=IFNA(LOOKUP(2,1/SEARCH(TableAccounts[Description Chart of Accounts],F2),TableAccounts[Description]),"""")"
that way I can check if it is column A [Description chart of Accounts] and/or column B [Accounts] in case it was typed manually in sheet1 column F.

But hey! Thank you very much for your help. I didn't know you could use excel formulas in VBA like that, like you did it. It is awesome.

I really appreciate your time, help and kindness Alex.
 
Upvote 0
Well I've just made it easier. I put all the accounts in the same column. However, because there are duplicated account numbers, I need to figure out 1st the DeptID to description will be the applicable one for the account. So I've added a column D on the Accounts sheet. Any suggestion on how to do this?

Working File.xlsx
ABCD
1Description Chart of AccountsAccountDescriptionDeptID
2O------ Administration ------O
35102 Travel - Admin5102Travel - Admin1XXX00X
45170 Vehicle Gasoline5170Vehicle Gasoline1XXX00X
55200 Operating Materials and Supplies5200Operating Materials and Supplies1XXX00X
65221 Teaching Literature and Supplies5221Teaching Literature and Supplies1XXX00X
75370 Telephone and Internet5370Telephone and Internet1XXX00X
85379 Postage and Mailing5379Postage and Mailing1XXX00X
95422 Public Affairs5422Public Affairs1XXX00X
105461 Bank Service Charges and Fees5461Bank Service Charges and Fees1XXX00X
115496 Luncheons, Socials, and Hosting5496Luncheons, Socials, and Hosting1XXX00X
125500 Miscellaneous5500Miscellaneous1XXX00X
135700 Small Office Equipment5700Small Office Equipment1XXX00X
145776 Small Office Equipment Maintenance5776Small Office Equipment Maintenance1XXX00X
155860 Small Purchases/Services for Mission Home and Office5860Small Purchases/Services for Mission Home and Office1XXX00X
165862 Rent5862Rent1XXX00X
175868 Utilities5868Utilities1XXX00X
18...
19O------ Y------O
205102 Travel - In field5102Travel - In field1XXX40X
215221 BOM (orders only)5221BOM (orders only)1XXX40X
225700 Furnishings5700Furnishings1XXX40X
235862 Rent 5862Rent 1XXX40X
245868 Utilities5868Utilities1XXX40X
255920 Charitable Assistance5920Charitable Assistance1XXX40X
265930 Food and Personal Items5930Food and Personal Items1XXX40X
27..
28O------ S ------O
295700 Furnishings5700Furnishings1XXX48X
305862 Rent5862Rent1XXX48X
315868 Utilities5868Utilities1XXX48X
32..
33O------ Vehicles ------O
345480 Vehicle Taxes and Fees5480Vehicle Taxes and Fees1XXX60X
355700 Vehicle Equipment5700Vehicle Equipment1XXX60X
365772 Vehicle Maintenance and Repairs5772Vehicle Maintenance and Repairs1XXX60X
37..
38O------ Other ------O
391020 BA1020BA1XXX90X
401096 Cash Transfers Clearing1096Cash Transfers Clearing1XXX90X
411220 Working Funds1220Working Funds1XXX90X
421300 Accounts Receivable1300Accounts Receivable1XXX90X
431925 Security Deposits1925Security Deposits1XXX90X
445102 Travel - Baggage, Visa, and Other5102Travel - Baggage, Visa, and Other1XXX90X
455949 Medical5949Medical1XXX90X
46O------ Administration ------O
475102Travel - Admin1XXX00X
485170Vehicle Gasoline1XXX00X
495200Operating Materials and Supplies1XXX00X
505221Teaching Literature and Supplies1XXX00X
515370Telephone and Internet1XXX00X
525379Postage and Mailing1XXX00X
535422Public Affairs1XXX00X
545461Bank Service Charges and Fees1XXX00X
555496Luncheons, Socials, and Hosting1XXX00X
565500Miscellaneous1XXX00X
575700Small Office Equipment1XXX00X
585776Small Office Equipment Maintenance1XXX00X
595860Small Purchases/Services for Mission Home and Office1XXX00X
605862Rent1XXX00X
615868Utilities1XXX00X
62O------ Y------O
635102Travel - In field1XXX40X
645221BOM (orders only)1XXX40X
655700Furnishings1XXX40X
665862Rent 1XXX40X
675868Utilities1XXX40X
685920Charitable Assistance1XXX40X
695930Food and Personal Items1XXX40X
70O------ S ------O
715700Furnishings1XXX48X
725862Rent1XXX48X
735868Utilities1XXX48X
74O------ Vehicles ------O
755480Vehicle Taxes and Fees1XXX60X
765700Vehicle Equipment1XXX60X
775772Vehicle Maintenance and Repairs1XXX60X
78O------ Other ------O
791020BA1XXX90X
801096Cash Transfers Clearing1XXX90X
811220Working Funds1XXX90X
821300Accounts Receivable1XXX90X
831925Security Deposits1XXX90X
845102Travel - Baggage, Visa, and Other1XXX90X
Accounts


DeptIDs consist of 2 parts as shown in Column D in the Accounts sheet.
- The first one is the first 4 digits [1XXX].
It will always start with a 1 and the other 3 digits will determine the country.

- The second part is the 3 last digits [XXX]
The first two digits will determine the category Admin, Y, S, Vehicles and Others. Being these (00, 40, 48, 60, 90)
And the last digit can be any number between 0 and 9 (most common a 0 and sometimes a 2 will be the most used ones)
 
Upvote 0
I
Well I've just made it easier. I put all the accounts in the same column. However, because there are duplicated account numbers, I need to figure out 1st the DeptID to description will be the applicable one for the account. So I've added a column D on the Accounts sheet. Any suggestion on how to do this?

Working File.xlsx
ABCD
1Description Chart of AccountsAccountDescriptionDeptID
2O------ Administration ------O
35102 Travel - Admin5102Travel - Admin1XXX00X
45170 Vehicle Gasoline5170Vehicle Gasoline1XXX00X
55200 Operating Materials and Supplies5200Operating Materials and Supplies1XXX00X
65221 Teaching Literature and Supplies5221Teaching Literature and Supplies1XXX00X
75370 Telephone and Internet5370Telephone and Internet1XXX00X
85379 Postage and Mailing5379Postage and Mailing1XXX00X
95422 Public Affairs5422Public Affairs1XXX00X
105461 Bank Service Charges and Fees5461Bank Service Charges and Fees1XXX00X
115496 Luncheons, Socials, and Hosting5496Luncheons, Socials, and Hosting1XXX00X
125500 Miscellaneous5500Miscellaneous1XXX00X
135700 Small Office Equipment5700Small Office Equipment1XXX00X
145776 Small Office Equipment Maintenance5776Small Office Equipment Maintenance1XXX00X
155860 Small Purchases/Services for Mission Home and Office5860Small Purchases/Services for Mission Home and Office1XXX00X
165862 Rent5862Rent1XXX00X
175868 Utilities5868Utilities1XXX00X
18...
19O------ Y------O
205102 Travel - In field5102Travel - In field1XXX40X
215221 BOM (orders only)5221BOM (orders only)1XXX40X
225700 Furnishings5700Furnishings1XXX40X
235862 Rent 5862Rent 1XXX40X
245868 Utilities5868Utilities1XXX40X
255920 Charitable Assistance5920Charitable Assistance1XXX40X
265930 Food and Personal Items5930Food and Personal Items1XXX40X
27..
28O------ S ------O
295700 Furnishings5700Furnishings1XXX48X
305862 Rent5862Rent1XXX48X
315868 Utilities5868Utilities1XXX48X
32..
33O------ Vehicles ------O
345480 Vehicle Taxes and Fees5480Vehicle Taxes and Fees1XXX60X
355700 Vehicle Equipment5700Vehicle Equipment1XXX60X
365772 Vehicle Maintenance and Repairs5772Vehicle Maintenance and Repairs1XXX60X
37..
38O------ Other ------O
391020 BA1020BA1XXX90X
401096 Cash Transfers Clearing1096Cash Transfers Clearing1XXX90X
411220 Working Funds1220Working Funds1XXX90X
421300 Accounts Receivable1300Accounts Receivable1XXX90X
431925 Security Deposits1925Security Deposits1XXX90X
445102 Travel - Baggage, Visa, and Other5102Travel - Baggage, Visa, and Other1XXX90X
455949 Medical5949Medical1XXX90X
46O------ Administration ------O
475102Travel - Admin1XXX00X
485170Vehicle Gasoline1XXX00X
495200Operating Materials and Supplies1XXX00X
505221Teaching Literature and Supplies1XXX00X
515370Telephone and Internet1XXX00X
525379Postage and Mailing1XXX00X
535422Public Affairs1XXX00X
545461Bank Service Charges and Fees1XXX00X
555496Luncheons, Socials, and Hosting1XXX00X
565500Miscellaneous1XXX00X
575700Small Office Equipment1XXX00X
585776Small Office Equipment Maintenance1XXX00X
595860Small Purchases/Services for Mission Home and Office1XXX00X
605862Rent1XXX00X
615868Utilities1XXX00X
62O------ Y------O
635102Travel - In field1XXX40X
645221BOM (orders only)1XXX40X
655700Furnishings1XXX40X
665862Rent 1XXX40X
675868Utilities1XXX40X
685920Charitable Assistance1XXX40X
695930Food and Personal Items1XXX40X
70O------ S ------O
715700Furnishings1XXX48X
725862Rent1XXX48X
735868Utilities1XXX48X
74O------ Vehicles ------O
755480Vehicle Taxes and Fees1XXX60X
765700Vehicle Equipment1XXX60X
775772Vehicle Maintenance and Repairs1XXX60X
78O------ Other ------O
791020BA1XXX90X
801096Cash Transfers Clearing1XXX90X
811220Working Funds1XXX90X
821300Accounts Receivable1XXX90X
831925Security Deposits1XXX90X
845102Travel - Baggage, Visa, and Other1XXX90X
Accounts


DeptIDs consist of 2 parts as shown in Column D in the Accounts sheet.
- The first one is the first 4 digits [1XXX].
It will always start with a 1 and the other 3 digits will determine the country.

- The second part is the 3 last digits [XXX]
The first two digits will determine the category Admin, Y, S, Vehicles and Others. Being these (00, 40, 48, 60, 90)
And the last digit can be any number between 0 and 9 (most common a 0 and sometimes a 2 will be the most used ones)
I've uploaded a MacroEnabled file in the Mega folder I shared which should have the VBA code.
 
Upvote 0
PS: If you interested in looking into Power Query, I posted this link in response to another question on the forum today.

This is an 18 min video from Wyn Hopkins which deals with using partial matching to map / categorise the information in the manner that you are doing.

The Power Query sounds really interesting. If I have some time this afternoon I will try to play with it. Thanks again for the reference.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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