Need a macro to conduct some steps for each part number in a column

MacroTestQ

New Member
Joined
Jun 30, 2022
Messages
4
Office Version
  1. 365
Platform
  1. MacOS
I have an Excel sheet with 12 columns and 4000+ rows. In Column E, I have a list of part numbers. These part numbers may or may not be repeated based on the city names found in Column F. For example, Part A (located in Column E) may be in 3 different rows: one row for City X, one row for City Y, and one row for City Z.

I would like to have a macro that goes through each part number and checks if there are 3 instances of that part number showing up: one associated with City X, one associated with City Y, and one associated with City Z.


If there are 3 instances, I would like the macro to make the cost the same for all the instances. The cost value is found in Column G.

If there are not 3 instances, I would like the macro to create a new row, or rows, and add the missing city, or cities in Column F to that row or rows.

Is this possible?


All columns start their data in Row 2 (Row 1 contains the headers).

Please view the attached images for a simplified example of what I currently have and what I'd like the end result to be.

Please let me know if you have any questions - I'd be happy to clarify.
 

Attachments

  • Current.png
    Current.png
    102.3 KB · Views: 14
  • End Result.png
    End Result.png
    148.3 KB · Views: 15
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Cannot manipulate data in a picture. Please reload with XL2BB so that we can help you without having to retype all your data.
 
Upvote 0
Hey Alan, thanks for your response. Please find my simplified example below:

Current:

Sample Sheet.xlsx
BCDEFGHIJKLM
1Column w/ Text 1Column w/ Text 2Column w/ Text 3ItemLocationCostColumn w/ Text 4Column w/ Text 5Column w/ Text 6Column w/ Text 7Column w/ Text 8Column w/ Text 9
2---X1City A$10------
3---X1City B$9------
4---X2City A$15------
5---X2City B$15------
6---X2City C$12------
7---X3City A$20------
8---X4City A$25------
9---X4City C$20------
10---X5City A$30------
Current


What I want the end result to be:

Sample Sheet.xlsx
BCDEFGHIJKLM
1Column w/ Text 1Column w/ Text 2Column w/ Text 3ItemLocationCostColumn w/ Text 4Column w/ Text 5Column w/ Text 6Column w/ Text 7Column w/ Text 8Column w/ Text 9
2---X1City A$10------
3---X1City B$10------
4---X1City C$10------
5---X2City A$15------
6---X2City B$15------
7---X2City C$15------
8---X3City A$20------
9---X3City B$20------
10---X3City C$20------
11---X4City A$25------
12---X4City C$25------
13---X4City B$25------
14---X5City A$30------
15---X5City B$30------
16---X5City C$30------
Needed
 
Upvote 0
In the new release of Excel 365 for MAC Insider, you have Power Query available to use. Here is the Mcode for Power Query to achieve your expected results.

Look here-->Import and shape data in Excel for Mac (Power Query)

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Item"}, {{"MaxCost", each List.Max([Cost]), type number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "City A", each "City A"),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "City B", each "City B"),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "City C", each "City C"),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Custom2", {"Item", "MaxCost"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Value"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Item", "Attribute", "MaxCost"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Attribute", "City"}})
in
    #"Renamed Columns"
 
Upvote 0
In the new release of Excel 365 for MAC Insider, you have Power Query available to use. Here is the Mcode for Power Query to achieve your expected results.

Look here-->Import and shape data in Excel for Mac (Power Query)

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Item"}, {{"MaxCost", each List.Max([Cost]), type number}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "City A", each "City A"),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "City B", each "City B"),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "City C", each "City C"),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Custom2", {"Item", "MaxCost"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Value"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Item", "Attribute", "MaxCost"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Attribute", "City"}})
in
    #"Renamed Columns"
Hey Alan, thanks for your help! Unfortunately, I don't have access to the Power Query Editor on my current Excel version. Although it is 16.62, the "Launch Power Query Editor" option doesn't show up under Get Data (Power Query). What can I do about this?
 
Upvote 0
Unfortunately, unless you sign up for the insider as indicated in the link, then PQ is not available to you. If you don't do that, then patience that someone will supply a VBA solution.
 
Upvote 0
Hey Alan, thanks for your help! Unfortunately, I don't have access to the Power Query Editor on my current Excel version. Although it is 16.62, the "Launch Power Query Editor" option doesn't show up under Get Data (Power Query). What can I do about this?
Please check your version and license type. You must have a subscription license type to have PowerQuery. Use the Menu Bar. Choose Excel > About Excel to display the version and license type. If your subscription is managed by a an education institution or IT department, they can turn PowerQuery off.
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,311
Members
449,080
Latest member
jmsotelo

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