Advanced filters & VBA code

John_Gil

New Member
Joined
Jun 3, 2020
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone! :)

I need to develop a macro that sums cases and packs (units) quantities from an exported file. The final document has the below aspect:

CasesUnits
WeekCases dispatchedCases forecastedCases orderedUnits despatchedUnits forecastedUnits ordered
35751777947892118785125137124681
Group A268027402746462724714947256
Group B274529203046450934866449939
Group C619695619109141223810914
Group D631566639597651866042
Group E167147167200417602004
Group F000000
Group G6757266758526101418526

Well each one of the groups have specific products associated what means that has to have specific filters from the exported file:
Group A - Filtered by "BACON" on column species and "OCADO" on column Sub-ProductTier;
Group B- Filtered by "Eden" on column Sub-ProductTier;
Group C- Filtered by "Organic" on column Sub-ProductTier;
Group D- Filtered by "PORK" on column species and "OCADO" on column Sub-ProductTier;
Group E- Filtered by "BACON" on column species;
Group F- Filtered by product code (RTO004, RTO005, RTO065, RTO008, RTO011,RTO012, RTO013);
Group G- Filtered by "LAMB" on column species and "OCADO" on column Sub-ProductTier;

The file from where this information needs to be taken has the below format:
ProductProductAreaCategorySpeciesSub-ProductTierProductTierCustomerFactTypeUnit
12/07/2020​
RTO140BURGERSFRESHBEEFOCADOCOREOCADO[Live] Combined HFI Forecast with PLM'sB
0​
RTO138SAUSAGEBREAKFASTPORKTESCO BRANDCOREOCADO[Live] Combined HFI Forecast with PLM'sB
0​
RTO137BURGERSFRESHBACONORGANICPREMIUMOCADO[Live] Combined HFI Forecast with PLM'sB
83​
RTO135JOINTFRESHLAMBEDENBRANDOCADO[Live] Combined HFI Forecast with PLM'sB
1​

The macro should first filter and then sum the cases and trays and present them into the first table.

Could you please help me with this guys? I'm just really starting to work with VBA and I would really appreciate your work.

Thank you,
John
 
When sharing the file, you do not need an email address, just choose share a link.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
OK perfect! So the links are below:
Source file Link (it comes from the export like this so first it requires a code to place from text to columns) :

Output file:

Thanks once again and let me know if you need any other information! I have generated a random export but this is how the data comes out. :)
 
Upvote 0
Hi John

The column FactType only contains references to Forecast, Delivered and Received. Additionally it only refers to Packs and Cases.

Can you clarify how Forecast, Delivered and Received relate to Despatched, Ordered and Forecast (the latter seems obvious, but best to be certain).
Are units the same as Cases?
Finally is 'Combined HFI Forecast with PLMs' referring to Cases?

Regards
 
Upvote 0
for now, I'm going to assume:

Forecast=Forecast
Received=Ordered
Delivered=Despatched

and

Cases=Cases
Packs=Units

Hope thats correct.
 
Upvote 0
Hi Peter,

Yes you are right. So in column H "[Live] Combined HFI Forecast with PLM's" is related to cases and "[Live] Combined HFI Forecast with PLM's in Packs" is related to the packs/units quantity.

Regarding your other question:
Delivered = Despatched
Received = Ordered
Forecast = Forecast

Regards,
John
 
Upvote 0
Well, this is proving harder than I thought - but not given up yet!

The first thing I'd recommend to you is to use Powerquery to convert your CSV file into excel. It can either be imported as a table or simply left in the Data Model to be accessed by the Pivottable. I assume you have a folder where each weeks data is located? If so the access could be adapted to collate all weeks data - this would allow a view to be taken across the year/month/week. For now though, From the 'Get & Transform' tab of the Data ribbon select 'New Query', 'From File', 'From CSV'. Navigate to the source of the data, and then click 'Transform Data' at bottom right of the dialogue that opens. This will put you in the PowerQuery Editor, at top left click 'Advanced Editor' and you should see something like this:

Code:
let
    Source = Csv.Document(File.Contents("C:\Users\pjmor\Desktop\MrExcel Stuff\ForecastAccuracyTest.csv"),[Delimiter=";", Columns=10, Encoding=1200, QuoteStyle=QuoteStyle.None]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}, {"Column10", type text}})
in
    #"Changed Type"

delete everyline from the first #'Changed Type' and replace with all but the first two lines of this:

Code:
let
    Source = Csv.Document(File.Contents("C:\Users\pjmor\Desktop\MrExcel Stuff\ForecastAccuracyTest.csv"),[Delimiter=";", Columns=10, Encoding=1200, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Product", type text}, {"ProductArea", type text}, {"Category", type text}, {"Species", type text}, {"Sub-ProductTier", type text}, {"ProductTier", type text}, {"Customer", type text}, {"FactType", type text}, {"Unit", type text}, {"12/07/2020", type number}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type1", "Forecast", each if Text.Contains([FactType], "Forecast") then [#"12/07/2020"] else null),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Despatched", each if Text.Contains([FactType], "Despatched") then [#"12/07/2020"] else null),
    #"Added Conditional Column2" = Table.AddColumn(#"Added Conditional Column1", "Ordered", each if Text.Contains([FactType], "Ordered") then [#"12/07/2020"] else null)
in
    #"Added Conditional Column2"

Click 'Done' in the bottom right corner. (if you now click on each step at the right of the editor you'll see the result at each step).
Click 'Close & Load' at top left and all the data will be loaded to a new sheet. To Load to Data Model, click the little down arrow next to 'Close & Load' and tick the box for add to data model in the dialogue and select 'Connection' only at the top - this is probably not what you want to do at the moment though.

My next step is to implement the relevant grouping and while I could hard code it I'd like to use a table like this to allow the definitions to be added to and/or amended:

Book.xlsx
MNOP
1Grouping Table
2GroupSpeciesSubProductTierProductCode
3Group ABACONOCADO
4Group BEden
5Group COrganic
6Group DPORKOCADO
7Group EBACON
8Group FRTO004, RTO005, RTO065, RTO008, RTO011,RTO012, RTO013
9Group GLAMBOCADO
Sheet1


But I'm struggling to implement it!

hope that helps a bit.
HTH
 
Upvote 0
Hi John, a question - what happens to records that have Bacon in the Species column? do the figures count in both Group A and Group E or in Group E if not OCADO, and Group A if they are?

Regards
 
Upvote 0
Hi Peter! Apologies my mistake, Group A should be "BEEF" and "OCADO". :)
 
Upvote 0
Hi John, The more I look at this the grouping seems very 'odd' - I know many businesses evolve and develop and some metrics look 'odd' to the outsider. But I do wonder whether a more consistent view might be obtained by using slicers on the Pivot table allowing companies/channels to be selected and the relevant product rather than forcing the groupings as this does. Just a thought as I'm getting to a solution, but not one I feel will prove reliable due to the awkward way I have had to apply the filter.

Grateful for your thoughts.
 
Upvote 0

Forum statistics

Threads
1,216,033
Messages
6,128,427
Members
449,450
Latest member
gunars

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