hello,
this is original thread
combining data in listbox on userform across multiple sheets and calculate values amongst them
and I add TOTAL columns in STA sheet
others sheets keep the same structure as in original thread
what I want sum and subtract TOTAL columns across sheets
the sheets should be =STA+RPA-SS (COLUMNS =TOTAL(I+H-H)) and divide result on QTY for column (10) in listbox
and Sheets should be =STA+SR-RR(COLUMNS =TOTAL(J+H-H)) and divide result on QTY for column (10) in listbox
ex: FR-1 ID= (2000+1200+1300)-(60+220)=4260 / 379=11.24010554 as show in column 11 in listbox
FR-1 ID=(3000+75)-(30+30)=3015/379 as show in column 12 .
here is result
to reminding what code really does it.
I have many sheets about five sheets contains data are almost 3000 rows for each sheet and it will increase continuously . so what I want when run the userform should merge the duplicate items based on COL B across the sheets each sheet repeat the items except the first sheet because this data collected from previous year. the others sheets are current year operations with considering the second sheet sometimes contains new item then should show in listbox . after merge duplicate items should show the QTY for each sheet . about COL 11 in list box and calculate like this as item (FR1)=200+200-5+4-20=379
as columns 11,12 in list box will calculate price average as I mentioned above
column 13 in list box =columns(12-11)* column(10).
as to selected sheet from combo box should follow the same way when calculation the price average when there is duplicates ID in each sheet alone without calculation across sheets.
the ID is not necessary to be existed in all sheets , sometimes there are new IDs in sheet and not existed in other sheets.
thanks.
this is original thread
combining data in listbox on userform across multiple sheets and calculate values amongst them
and I add TOTAL columns in STA sheet
KM.xlsm | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | ITEM | ID | BR | TY | OR | QTY | UNIT COST | UNIT SALE | COST TOTAL | SALES TOTAL | ||
2 | 1 | FR-1 | FR | BANANA | TT | 200.00 | 10.00 | 15.00 | 2,000.000 | 3,000.00 | ||
3 | 2 | FR-2 | FR | APPLE | LL | 100.00 | 11.00 | 17.00 | 1,100.000 | 1,700.00 | ||
4 | 3 | FR-3 | FR | PEAR | NN | 60.00 | 12.00 | 15.00 | 720.000 | 900.00 | ||
5 | 4 | FR-4 | FR | BANANA | 55.00 | 13.00 | 17.00 | 715.000 | 935.00 | |||
6 | 5 | VEG1 | VEG | TOMATO | SS | 50.00 | 14.00 | 16.00 | 700.000 | 800.00 | ||
7 | 6 | VEG2 | VEG | TOMATO | AA | 50.00 | 11.00 | 15.00 | 550.000 | 750.00 | ||
8 | 7 | FR-5 | FR1 | PEAR | MM | 20.00 | 9.00 | 18.00 | 180.000 | 360.00 | ||
9 | 8 | FR-6 | FR2 | PEAR | MM | 20.00 | 10.00 | 18.00 | 200.000 | 360.00 | ||
STA |
Cell Formulas | ||
---|---|---|
Range | Formula | |
I2:I9 | I2 | =F2*G2 |
J2:J9 | J2 | =H2*F2 |
others sheets keep the same structure as in original thread
KM.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | UNIT COST | TOTAL | ||
2 | 01/01/2021 | FR-1 | FR | BANANA | TT | 100.00 | 12.00 | 1,200.00 | ||
3 | 01/02/2021 | FR-2 | FR | APPLE | LL | 50.00 | 15.00 | 750.00 | ||
4 | 01/03/2021 | FR-3 | FR | PEAR | NN | 60.00 | 12.00 | 720.00 | ||
5 | 01/04/2021 | FR-4 | FR | BANANA | 60.00 | 13.00 | 780.00 | |||
6 | 01/05/2021 | VEG1 | VEG | TOMATO | SS | 65.00 | 14.00 | 910.00 | ||
7 | 01/06/2021 | VEG2 | VEG | TOMATO | AA | 40.00 | 11.00 | 440.00 | ||
8 | 01/07/2021 | FR-1 | FR | BANANA | TT | 100.00 | 13.00 | 1,300.00 | ||
9 | 01/08/2021 | FR-5 | FR1 | PEAR | MM | 55.00 | 14.00 | 770.00 | ||
RPA |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H2:H9 | H2 | =G2*F2 |
KM.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | UNIT SALE | TOTAL | ||
2 | 02/01/2021 | FR-1 | FR | BANANA | TT | 5.00 | 15.00 | 75.00 | ||
3 | 02/03/2021 | FR-3 | FR | PEAR | NN | 5.00 | 15.00 | 75.00 | ||
4 | 02/04/2021 | FR-4 | FR | BANANA | 2.00 | 17.00 | 34.00 | |||
5 | 02/05/2021 | VEG1 | VEG | TOMATO | SS | 3.00 | 16.00 | 48.00 | ||
6 | 02/06/2021 | VEG2 | VEG | TOMATO | AA | 4.00 | 15.00 | 60.00 | ||
7 | 02/08/2021 | FR-5 | FR1 | PEAR | MM | 2.00 | 15.00 | 30.00 | ||
8 | 02/08/2021 | FR-5 | FR1 | PEAR | MM | 2.00 | 20.00 | 40.00 | ||
SR |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H2:H8 | H2 | =G2*F2 |
KM.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | PRICE | TOTAL | ||
2 | 03/01/2021 | FR-1 | FR | BANANA | TT | 2.00 | 15.00 | 30.00 | ||
3 | 03/02/2021 | FR-3 | FR | PEAR | NN | 2.00 | 15.00 | 30.00 | ||
4 | 03/03/2021 | FR-1 | FR | BANANA | TT | 2.00 | 15.00 | 30.00 | ||
5 | 03/04/2021 | FR-3 | FR | PEAR | NN | 2.00 | 15.00 | 30.00 | ||
RR |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H2:H5 | H2 | =G2*F2 |
KM.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | PRICE | TOTAL | ||
2 | 01/01/2021 | FR-1 | FR | BANANA | TT | 5.000 | 12.000 | 60.000 | ||
3 | 01/02/2021 | FR-2 | FR | APPLE | LL | 10.000 | 11.000 | 110.000 | ||
4 | 01/03/2021 | FR-1 | FR | BANANA | TT | 15.000 | 12.000 | 180.000 | ||
5 | 01/04/2021 | FR-2 | FR | APPLE | LL | 20.000 | 11.000 | 220.000 | ||
6 | 02/04/2021 | FR-6 | FR2 | PEAR | MM | 10.00 | 8.00 | 80.000 | ||
7 | ||||||||||
SS |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H2:H6 | H2 | =G2*F2 |
what I want sum and subtract TOTAL columns across sheets
the sheets should be =STA+RPA-SS (COLUMNS =TOTAL(I+H-H)) and divide result on QTY for column (10) in listbox
and Sheets should be =STA+SR-RR(COLUMNS =TOTAL(J+H-H)) and divide result on QTY for column (10) in listbox
ex: FR-1 ID= (2000+1200+1300)-(60+220)=4260 / 379=11.24010554 as show in column 11 in listbox
FR-1 ID=(3000+75)-(30+30)=3015/379 as show in column 12 .
here is result
to reminding what code really does it.
I have many sheets about five sheets contains data are almost 3000 rows for each sheet and it will increase continuously . so what I want when run the userform should merge the duplicate items based on COL B across the sheets each sheet repeat the items except the first sheet because this data collected from previous year. the others sheets are current year operations with considering the second sheet sometimes contains new item then should show in listbox . after merge duplicate items should show the QTY for each sheet . about COL 11 in list box and calculate like this as item (FR1)=200+200-5+4-20=379
as columns 11,12 in list box will calculate price average as I mentioned above
column 13 in list box =columns(12-11)* column(10).
as to selected sheet from combo box should follow the same way when calculation the price average when there is duplicates ID in each sheet alone without calculation across sheets.
the ID is not necessary to be existed in all sheets , sometimes there are new IDs in sheet and not existed in other sheets.
VBA Code:
Sub LoadListbox()
Dim sh1 As Worksheet, sh As Worksheet
Dim a As Variant, b() As Variant, c As Variant, d As Variant, e As Variant
Dim dic As Object
Dim arSh As Variant, itSh As Variant
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim p As Long, q As Long, u As Long
Dim x1 As Double, x2 As Double, x3 As Double, y1 As Double, y2 As Double, y3 As Double
Set dic = CreateObject("Scripting.Dictionary")
'first sheet
a = Sheets("STA").Range("A2", Sheets("STA").Range("H" & Rows.Count).End(3)).Value
'second sheet
d = Sheets("RPA").Range("A1", Sheets("RPA").Range("H" & Rows.Count).End(3)).Value
'Sheet names, from 2 to last
arSh = Array("RPA", "SR", "RR", "SS")
'
u = UBound(arSh) + 2
ReDim c(1 To UBound(a, 1) + UBound(d, 1), 1 To 9 + u)
ListBox1.ColumnCount = 9 + u
m = 6 'Initial column inside the listbox for the sheets
'
'For the first sheet
For i = 1 To UBound(a)
dic(a(i, 2)) = i
For j = 1 To 6 'UBound(a, 2)
c(i, j) = a(i, j)
Next
c(i, m + u) = a(i, 6) 'qty
c(i, m + u + 1) = "1|" & a(i, 7) 'unit cost
c(i, m + u + 2) = "1|" & a(i, 8) 'unit sale
'c(i, m + u + 3) = (c(i, m + u + 2) - c(i, m + u + 1)) * c(i, m + u)
Next i
'
'For the second sheet
p = dic.Count 'Number of indices
For i = 2 To UBound(d)
If Not dic.exists(d(i, 2)) Then
p = p + 1
dic(d(i, 2)) = p
For j = 1 To 5
c(p, j) = d(i, j)
Next j
If d(1, 7) = WorksheetFunction.Trim("UNIT COST") Then
c(p, m + u + 1) = "1|" & d(i, 7)
ElseIf WorksheetFunction.Trim("UNIT SALE") Then
c(p, m + u + 2) = "1|" & d(i, 7)
End If
End If
Next i
'
n = 7 'To increase the column for each sheet
q = 1 'If it's odd or even
For itSh = 0 To UBound(arSh)
Set sh = Sheets(arSh(itSh))
q = q + 1
Erase b()
b = sh.Range("A1", sh.Range("H" & Rows.Count).End(3)).Value
For i = 2 To UBound(b)
If dic.exists(b(i, 2)) Then
k = dic(b(i, 2))
c(k, n) = c(k, n) + b(i, 6)
If q Mod (2) = 0 Then
c(k, m + u) = c(k, m + u) + b(i, 6)
Else
c(k, m + u) = c(k, m + u) - b(i, 6)
End If
x1 = Split(c(k, m + u + 1), "|")(0)
x2 = Split(c(k, m + u + 1), "|")(1)
y1 = Split(c(k, m + u + 2), "|")(0)
y2 = Split(c(k, m + u + 2), "|")(1)
If b(1, 7) = WorksheetFunction.Trim("UNIT COST") Then
x1 = x1 + 1
x2 = x2 + b(i, 7)
c(k, m + u + 1) = x1 & "|" & x2
ElseIf b(1, 7) = WorksheetFunction.Trim("UNIT SALE") Then
y1 = y1 + 1
y2 = y2 + b(i, 7)
c(k, m + u + 2) = y1 & "|" & y2
End If
End If
Next
n = n + 1
Next
'
ReDim e(1 To dic.Count, 1 To UBound(c, 2))
For i = 1 To dic.Count
For j = 1 To 5
e(i, j) = c(i, j)
Next
For j = 6 To 6 + u
e(i, j) = Format(c(i, j), "0.00; -0.00; -")
If e(i, j) = "" Or e(i, j) = 0 Then e(i, j) = "-"
Next
x1 = Split(c(i, m + u + 1), "|")(0)
x2 = Split(c(i, m + u + 1), "|")(1)
If x1 > 0 Then
x3 = x2 / x1
e(i, m + u + 1) = Format(x3, "$#,##0.00; -$#,##0.00; -")
End If
y1 = Split(c(i, m + u + 2), "|")(0)
y2 = Split(c(i, m + u + 2), "|")(1)
If y1 > 0 Then
y3 = y2 / y1
e(i, m + u + 2) = Format(y3, "$#,##0.00; -$#,##0.00; -")
End If
e(i, m + u + 3) = Format((y3 - x3) * e(i, m + u), "$#,##0.00; -$#,##0.00; -")
Next
ListBox1.RowSource = ""
ListBox1.List = e
End Sub
Private Sub ComboBox1_Change()
With ComboBox1
If .Value = "" Then
Call LoadListbox
Exit Sub
End If
If .ListIndex = -1 Then Exit Sub
ListBox1.RowSource = ""
ListBox1.Clear
ListBox1.RowSource = "'" & Sheets(.Value).Name & "'!" & Sheets(.Value).Range("A2", Sheets(.Value).Range("H" & Rows.Count).End(3)).Address
End With
End Sub
Private Sub UserForm_Activate()
Call LoadListbox
End Sub
Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "RPA"
.AddItem "SS"
.AddItem "SR"
.AddItem "RR"
End With
End Sub
Last edited: