MACRO to Run an Index Match for Multiple Values, returning multiple Rows

akasnett

New Member
Joined
Jun 17, 2020
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Sorry about the long title. After spending all morning on this it seems that this is more complicated then I thought it would be...

I attached an image with my spread sheet.

Column A is a list of full zip codes that I have from my system
Column B is the first 3 char's of each of those
-
Column C is the first 3 chars of a zip provided by the carrier i am using
Column D is the code for that zip.

What is need is the full zip that starts with the first 3 characters provided by the carrier (THERE WILL BE MULTIPLE RESULTS)
And the corresponding code from Column D.
Any way to do this not line by line copying the index of column C into the formula is have in J2 and dragging to get all the results and then copying it to a new sheet with the code and moving to the next row etc etc....
 

Attachments

  • excel.png
    excel.png
    153.4 KB · Views: 11

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I will be happy to help you if I can.
What I need from you based on the image is the exact output you want based on 005 or 010... 005 will be easier... so I can begin with the end in mind.
013 is not in the image to the left so less helpful
 
Upvote 0
Thanks so much for trying to help!

Snippet Sample :
Zip CodeLEFTDest. ZIPGround
00501005004002
00544005005002
01001010006045
01002010007045
01003010009045
01004010010002


I want to see:
ZipGroundCode
00501002
00544002

So I am looking up the Dest. Zip in the column called LEFT and returning the Zip Code, but being the amount of records, its taking forever to do it so many times

Example Formula:
=
INDEX($A$2:$A$40472,AGGREGATE(15,6,($B$2:$B$40471=$C$10)/($B$2:$B$40471=$C$10)*ROW(FullZip),ROWS($J3:J$7)))

but for each new prefix, I am changing the bolded text.....

I dont see a way for me to attach my whole file
 
Upvote 0
I was overcomplicating it.
I did it the opposite way... I pull the first 3 of the zip to match the prefix and then pulled the code...

Tho the prefix's originally came in ranges, i wish thier was a way to expand things like this automatically
Zip PrefixCode
875-877006
878-880007
881-882006
883007
884006
885007
889-961008
970-986008
988-994008

for example on the first line, i need to expand it to its own row for
875 006
876 006
877 006
 
Upvote 0
VBA Code:
Sub getItTogether()
'
'   by BrainDiesel
'
    Range("C2").Select
    Application.ScreenUpdating = False
    myNow = ActiveCell.Row
    Do Until Cells(myNow, 1).Value = ""
    ActiveCell.Formula = "=+LEFT(A" & myNow & ",3)"
    ActiveCell.Offset(0, 1).Formula = "=if(LEN(A" & myNow & ")=3,0,RIGHT(A" & myNow & ",len(A" & myNow & ")-FIND(""-"",A" & myNow & ",1))-LEFT(A" & myNow & ",3))"
    myRows = ActiveCell.Offset(0, 1).Value
    ActiveCell.Offset(0, 1).ClearContents
    Selection.Value = Selection.Value
    If myRows > 0 Then Selection.AutoFill Destination:=Range(Selection, Selection.Offset(myRows, 0)), Type:=xlFillSeries
    Selection.Value = Selection.Value & " " & Format(Cells(myNow, 2).Value, "000")
    ActiveCell.Offset(1, 0).Select
    Do Until myRows = 0
        Selection.Value = Selection.Value & " " & Format(Cells(myNow, 2).Value, "000")
        myRows = myRows - 1
        ActiveCell.Offset(1, 0).Select
       
    Loop
    myNow = myNow + 1
   
    Loop
    Application.ScreenUpdating = True
    Range("A1").Select
   
End Sub
Book2
AB
1Zip PrefixCode
2875-8776
3878-8807
4881-8826
58837
68846
78857
8889-9618
9970-9868
10988-9948
Sheet1

The code above on this spreadsheet seems to give what you are looking for
This code presumes always having the firse three characters included... if you have 1000-1004, the starting point will be 100, not 1000... first three characters... is that a concern?

Looping adds time so there may be a faster way, but this is my solution otherwise

Hope this solves your problem
 
Upvote 0

Forum statistics

Threads
1,215,043
Messages
6,122,825
Members
449,096
Latest member
Erald

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