Compiling Data From A Filter Worksheet

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this filtered data (autofilter) from my worksheet.

2021-2022 Data.xlsx
ABLNO
1RIDDateAcctSaltSand
11444549W0012021-12-19CW0.5
11544549W0022021-12-19404MIN
11644549W0032021-12-19AMCC0.25
11744549W0042021-12-19WP0.25
11844549W0052021-12-19CCGGMIN
11944549W0062021-12-19CW0.25
12044549W0072021-12-19CW0.25
12144549W0082021-12-19404MIN
12244549W0092021-12-19AMCCMIN
12344549W0102021-12-19MSCCMIN
12444549W0112021-12-19WMRC0.25
12544549W0122021-12-19WP0.25
12644549W0132021-12-19HSPMIN
12744549W0142021-12-19SCMIN
12844549W0152021-12-19CW0.25
12944549W0162021-12-19BIAMIN
13044549W0172021-12-19ARCMIN
13144549W0182021-12-19CCGGMIN
13244549W0192021-12-19WMRCMIN
13344549W0202021-12-19WTSMIN
13444549W0212021-12-19SP0.25
13544549W0222021-12-19WPMIN
13644550W0012021-12-20CW0.25
13744550W0022021-12-20SPMIN
13844550W0032021-12-20CW0.25
13944550W0042021-12-20CW0.5
14044550W0052021-12-20CW0.5
14144550W0062021-12-20SP0.25
14244551W0012021-12-21CW0.25
14344551W0022021-12-21BIAMIN
14444551W0032021-12-21ARCMIN
14544551W0042021-12-21CCGGMIN
14644551W0052021-12-21WTSMIN
14744551W0062021-12-21SPMIN
14844551W0072021-12-21CW0.25
14944551W0082021-12-21SP0.25
15044551W0092021-12-21CW0.25
15144551W0102021-12-21SP0.25
15244551W0112021-12-21CW0.25
15344551W0122021-12-21CW0.25
15444551W0132021-12-21SP0.25
15544552W0012021-12-22CW0.25
15644552W0022021-12-22SP0.25
15744552W0032021-12-22CWMIN
15844552W0042021-12-22CW0.25
15944552W0052021-12-22SPMIN
16044552W0062021-12-22CWMIN
16144552W0072021-12-22SP0.25
16244552W0082021-12-22SP0.5
16344552W0092021-12-22CWMIN
16444552W0102021-12-22ARCMIN
16544552W0112021-12-22UPPMIN
16644552W0122021-12-22WTSMIN
16744552W0132021-12-22SP0.25
16844553W0012021-12-23CW0.25
16944553W0022021-12-23SPMIN
17044553W0032021-12-23CW0.25
17144553W0042021-12-23CW0.25
17244553W0052021-12-23SP0.25
17344553W0062021-12-23CW0.5
17444553W0072021-12-23SP0.5
17544554W0012021-12-24CW
17644554W0022021-12-24CW
17744554W0032021-12-24CW
17844554W0042021-12-24CW
OPS


I am looking for a VBA solution to get a list of unique values from Column L (Acct). This compiled list will be placed in worksheet "WEEKS", at cell A32.
Then, for each unique value from column L (acct), get the sum of salt and the sum of sand, and the count of the number of occurrences of "MIN", and place the values in "WEEKS", at cells B32, C32 and D32 respectively.

2021-2022 Data.xlsx
ABCD
31Sum SaltSum SandCount "MIN"
324042
33AMCC0.2501
34ARC3
35BIA 2
36CCGG3
37CW5.7503
38HSP1
39MSCC1
40SC1
41SP3.2504
42UPP1
43WMRC0.2501
44WP0.5001
45WTS3
WEEKS
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
What is the name of the worksheet with the filtered data?
 
Upvote 0
Try this:

VBA Code:
Sub CompilingData()
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Sheets("OPS").Range("L2:O" & Sheets("OPS").Range("L" & Rows.Count).End(3).Row).SpecialCells(xlCellTypeVisible).Value
  ReDim b(1 To UBound(a, 1), 1 To 4)
  For i = 1 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then dic(a(i, 1)) = dic.Count + 1
    j = dic(a(i, 1))
    b(j, 1) = a(i, 1)
    If a(i, 3) <> "MIN" Then b(j, 2) = b(j, 2) + a(i, 3) Else b(j, 4) = b(j, 4) + 1
    If a(i, 4) <> "" Then b(j, 3) = b(j, 3) + a(i, 4)
  Next
  With Sheets("WEEKS")
    .Range("A32").Resize(dic.Count, 4).Value = b
    .Range("A31").Resize(dic.Count + 1, 4).Sort Key1:=.Range("A31"), Order1:=xlAscending, Header:=xlYes
  End With
End Sub
 
Upvote 0
Solution
Hello Dante. Thank you so much for spending your time sharing your skill with me. It works wonderfully.
I'm wondering if you have a moment to explain to me how it works.

From what I can gather, "dic" is a "custom dictionary" of sorts that holds the unique values found in column L as they are encountered.
"a" is the filtered column (L) of data? Not the usual way I see a range defined if that is what it is. I don't know what kind of value 'a' holds if any.
Then it looks like an array is created (b?). This is a new concept for me so 'ReDim b(1 To UBound(a, 1), 1 To 4)' is a bit elusive to me.
What happens in the loop I have no idea. This is where all the analysis happens, but I can't understand the references. (likely because they are related to the array?) Seeing If/Then/Else on in one line is also pretty cool.
FInally, the compiled data is presented on the new worksheet. The resize is throwing me off.
 
Upvote 0
From what I can gather, "dic" is a "custom dictionary" of sorts that holds the unique values found in column L as they are encountered.
It is right

In the array"a" the data of "L" are loaded but only the visible ones.

Then it looks like an array is created (b?)
In array "b" the result will be stored.

In each key of the dictionary you can store a data, in this case the data that I am storing is the number of unique data, which I use later as the row number in the array "b".
So if I have 15 records, with 2 keys, in row 1 of array "b" the data of the first key goes, in row 2 of array "b" the data of the second key goes.

j = dic(a(i, 1))
So, according to the previous explanation, with the previous line the row number of the key is obtained.
In that line the calculations are put: sums or counts.

Finally, in the WEEKS sheet the array is downloaded.
 
Upvote 0
Thank you once again Dante for providing that explanation. I'll have to digest it a bit more and maybe play around with it a bit more to get a better working knowledge of it. It's a good start though!
 
Upvote 0
I have run into a situation where I am needing to add to the code provided above. Unfortunately, I still don't have enough understanding of the array aspect to make my changes work. I've only managed to mess things up.

I have columns for both sand and salt. Each one can have the value "MIN" in them. The code Dante provided for me gives me the sum of each salt and sand and the count of "MIN" for what I believe to be from both salt and sand columns(?)

In addition to the sums of both salt and sand columns, I need the counts of "MIN" for both salt and sand as seperate values.

Destinations columns B will be the sum of salt values; C wioll be the count of "MIN" in the salt column; D the sum of sand values; and E the count of "MIN" in the sand column.
 
Upvote 0

Forum statistics

Threads
1,221,448
Messages
6,159,922
Members
451,604
Latest member
SWahl

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