VBA to filter on 2 columns and consolidate data into one row

sncb

Board Regular
Joined
Mar 17, 2011
Messages
168
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
I have data sets that look like this below and pasted from A2 onwards with fixed no of columns but varying no of rows..

1617287505419.png


and need to process it in such a way that VB would first filter on Col J and then on Col E for unique values (like below)

1617287761061.png


and from the resulting value remove duplicates and consolidate it into the top row while summing up totals in col L for only the filtered values as shown:

1617288024781.png


and then continue the process to filter on next value of E (while still filtered on the first value of J)

1617288271988.png


to get:
1617288509918.png


and then the filter on next value in Col J and Col E again and so on...such that when i paste the data and run VB, I am able to get something like this:

1617289114604.png


I know this is a bit complicated but if anyone could help out this would be great for my work purposes. I've tried recording the process as a macro but it records actual values which I cannot use as the data in each such data set that i process has different values.

Tia to anyone who could help out.

Here is the raw data if it might help.

CcodeCountryCodeShortRegionZipStateDogIDDogTypeOwnerColorContinentValue
1​
USAUnited StatesNorthAm
99999​
Texas
4412​
TerrierMartinezGreenAmericas
5​
1​
USAUnited StatesNorthAm
99999​
Texas
4412​
TerrierHernandezGreenAmericas
10​
1​
USAUnited StatesNorthAm
99999​
Texas
4916​
GerShepLopezGreenAmericas
15​
1​
USAUnited StatesNorthAm
77777​
Michigan
4916​
GerShepGonzalezGreenAmericas
20​
1​
USAUnited StatesNorthAm
77777​
Michigan
4919​
GerShepWilsonGreenAmericas
5​
1​
USAUnited StatesNorthAm
77777​
Michigan
4920​
CollieJohnsonGreenAmericas
10​
1​
USAUnited StatesNorthAm
77777​
Michigan
4920​
CollieWilliamsGreenAmericas
15​
1​
USAUnited StatesNorthAm
55555​
Georgia
5326​
DobermanBrownWhiteAmericas
20​
1​
USAUnited StatesNorthAm
55555​
Georgia
5326​
DobermanJonesWhiteAmericas
5​
1​
USAUnited StatesNorthAm
55555​
Georgia
7060​
StrayGarciaWhiteAmericas
10​
1​
USAUnited StatesNorthAm
55555​
Georgia
7060​
StrayMillerWhiteAmericas
15​
1​
USAUnited StatesNorthAm
33333​
Montana
8837​
PomDavisWhiteAmericas
20​
1​
USAUnited StatesNorthAm
33333​
Montana
3249​
HoundRodriguezWhiteAmericas
5​
1​
USAUnited StatesNorthAm
33333​
Montana
8495​
GoldRetSmithWhiteAmericas
10​
 

Attachments

  • 1617287990662.png
    1617287990662.png
    18.6 KB · Views: 8
  • 1617288230858.png
    1617288230858.png
    14.9 KB · Views: 7
  • 1617288826105.png
    1617288826105.png
    59.4 KB · Views: 9
Last edited:
Hi Peter,

  1. Column L
  2. When I select the full column it says 'General' on the ribbon but when I select each cell or all the cells with data (only the cells not the column) it says 'Numeric' on the ribbon.
1617618978596.png


However I'm not able to replicate the error with this below data although with the original data, the error pops up. Ive ensured the column type and cell types as described in #2 above are similar in both the data sets (original and test data). Ive even tried pasting data as values but same error.

Here is some raw data although I simply changed the values to have some 0s in Col L.

CcodeCountryCodeShortRegionZipStateDogIDDogTypeOwnerColorContinentValue
1​
USAUnited StatesNorthAm
99999​
Texas
4412​
TerrierMartinezGreenAmericas
0​
1​
USAUnited StatesNorthAm
99999​
Texas
4412​
TerrierHernandezGreenAmericas
0​
1​
USAUnited StatesNorthAm
99999​
Texas
4916​
GerShepLopezGreenAmericas
15​
1​
USAUnited StatesNorthAm
77777​
Michigan
4916​
GerShepGonzalezGreenAmericas
0​
1​
USAUnited StatesNorthAm
77777​
Michigan
4919​
GerShepWilsonGreenAmericas
5​
1​
USAUnited StatesNorthAm
77777​
Michigan
4920​
CollieJohnsonGreenAmericas
0​
1​
USAUnited StatesNorthAm
77777​
Michigan
4920​
CollieWilliamsGreenAmericas
15​
1​
USAUnited StatesNorthAm
55555​
Georgia
5326​
DobermanBrownWhiteAmericas
0​
1​
USAUnited StatesNorthAm
55555​
Georgia
5326​
DobermanJonesWhiteAmericas
5​
1​
USAUnited StatesNorthAm
55555​
Georgia
7060​
StrayGarciaWhiteAmericas
10​
1​
USAUnited StatesNorthAm
55555​
Georgia
7060​
StrayMillerWhiteAmericas
0​
1​
USAUnited StatesNorthAm
33333​
Montana
8837​
PomDavisWhiteAmericas
0​
1​
USAUnited StatesNorthAm
33333​
Montana
3249​
HoundRodriguezWhiteAmericas
5​
1​
USAUnited StatesNorthAm
33333​
Montana
8495​
GoldRetSmithWhiteAmericas
10​



onary") a = Sheets("Sheet1").Range("A1", Sheets("Sheet1")
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Run the code on the problem data again and when it errors and you Debug, hover over the variable "i" in that yellow line and check what value shows in the pop-up.
If it is, say 123, then go to that row in your sheet and look closely at the column L value and see what is in that cell. At this stage I suspect the likely culprit is a text value in that cell or an error value.
 
Upvote 0
Run the code on the problem data again and when it errors and you Debug, hover over the variable "i" in that yellow line and check what value shows in the pop-up.
If it is, say 123, then go to that row in your sheet and look closely at the column L value and see what is in that cell. At this stage I suspect the likely culprit is a text value in that cell or an error value.
Got it. Turns out it was a blank cell. I went ahead and filled blank cells with a 0 and it worked great.

Thanks again Peter. (y)(y)(y)
 
Upvote 0
Got it. Turns out it was a blank cell.
A truly blank cell would not cause that error. There must be something in that cell, though it possibly may not be visible.


I went ahead and filled blank cells with a 0 and it worked great.
That would remove whatever was in there and replace it with 0.
That's fine if this is a one-off job or if you are happy to continually do that otherwise we could continue to delve so that eventually the macro would just do the whole job.
 
Upvote 0
Actually, just try adding this blue text and running on the original data again.

Rich (BB code):
If IsNumeric(a(i, 12)) Then d4(s) = d4(s) + a(i, 12)
 
Upvote 0
Hi Peter,

Was just writing to you that I can manage with what you provided already but your latest post with the code edit works perfectly. :)

Thank you once again Peter. Really appreciate your effort towards helping me out here. ?
 
Upvote 0
Hi Peter,

Was just writing to you that I can manage with what you provided already but your latest post with the code edit works perfectly. :)

Thank you once again Peter. Really appreciate your effort towards helping me out here. ?
You are very welcome. Glad it helped. :)

@Peter_SSs Thanks for the heads-up on XL2BB.
(y)
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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