• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Macro to compare a column with a list of values

gtabib

New Member
Hi everyone. I am new with VBA macro and need help to address a work issue.
I have a column in my data set and need to compare the values in this column (HHS) with a master list and insert a blank row every time the data is missing in column HHS.
I know there are already other posts regarding this, but I couldn't find the exact match for my requirement.
Below is my original data set along with the master list (lookup list) and expected result.

Original data
MonthHHSMonth orderEmergency Claimed $
JulyCairns and Hinterland1.000
JulyCentral Queensland1.0012
JulyDarling Downs1.0041
JulyGold Coast1.0011
JulyMetro North1.00111
JulyMetro South1.0014
JulySunshine Coast1.0041
JulyTownsville1.0021
JulyWest Moreton1.0047
JulyWide Bay1.0023
AugustCairns and Hinterland2.0052
AugustCentral Queensland2.0056
AugustDarling Downs2.0045
AugustGold Coast2.0041
AugustMetro North2.0012
AugustMetro South2.0045
AugustSunshine Coast2.0045
AugustTownsville2.000
AugustWest Moreton2.0012

Master List that column B (HHS) needs to be compared with:
Carins and Hinterland
Central Queensland
Central West
Darling Downs
Gold Coast
Mackay
Metro North
Metro South
North West
South West
Sunshine Coast
Torres and Cape
Townsville
West Moreton
Wide Bay

Expected Result (a loop to return to the first row after each month and check the next month)
MonthHHSMonth OrderEmergency Claimed $
JulyCarins and Hinterland10
JulyCentral Queensland112
JulyDarling Down141
JulyGold Coast111
JulyMetro North1111
JulyMetro South114
JulySunshine Coast141
JulyTownsville121
JulyWest Moreton147
JulyWide Bay123

Appreciate any help!
 
A power query solution

Code:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    MQ = Table.NestedJoin(T1, {"HHS"}, T2, {"Column1"}, "T3", JoinKind.RightOuter),
    #"Expanded T3" = Table.ExpandTableColumn(MQ, "T3", {"Column1"}, {"Column1"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded T3", each ([Month] <> null)),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Month", Order.Descending}, {"HHS", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Column1"})

in
    #"Removed Columns"
 

Attachments

  • PQ-Join.xlsx
    18.9 KB · Views: 2
Likewise a Power Query solution in the attached. See table at cell H1.
Needs refreshing like a pivot table (right-click and choose Refresh)
I've left HHS showing so you can see what's missing for what month.

1708948136613.png
 

Attachments

  • Chandoo56252.xlsx
    19.8 KB · Views: 1

gtabib

Here a sample VBA solution.
It shows also those HHSs, which don't match with Your data.
Select Result-sheet to see results.
 

Attachments

  • gtabib.xlsb
    22.2 KB · Views: 5

gtabib

Here a sample VBA solution.
It shows also those HHSs, which don't match with Your data.
Select Result-sheet to see results.
Thanks so much for your help. It's awesome, can you please review your code because it ignores the first HHS which is Cairns and Hinterland. In my original data this HHS exist (in first table) however when running your code the result tab does not show this HHS. The code is perfectly working though! Thanks again!
 
Last edited:
A power query solution

Code:
let
    T1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    MQ = Table.NestedJoin(T1, {"HHS"}, T2, {"Column1"}, "T3", JoinKind.RightOuter),
    #"Expanded T3" = Table.ExpandTableColumn(MQ, "T3", {"Column1"}, {"Column1"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded T3", each ([Month] <> null)),
    #"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Month", Order.Descending}, {"HHS", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Column1"})

in
    #"Removed Columns"
Thanks so much for your help!
 

gtabib

Your it ignores the first HHS which is Cairns and Hinterland.
... because texts are different Cairns <> Carins .
DataScreenshot 2024-02-29 at 09.44.15.png
HHSScreenshot 2024-02-29 at 09.43.58.png
Should I guess Your original data?
or compare number of letters?
 
Back
Top