Hel
the orginal thread got help from @DanteAmore.combining data in listbox on userform across multiple sheets and calculate values amongst them
I hope from him or anybody can help.
the orginal code will calculation the stock across sheets with merge duplicate items for each sheet if they repeat in the same sheet based on column B .so what I want ignore columns unit price whether cost or sale . instead of that I want show COST TOTAL & SALES TOTAL but should merge for MULTIPLE sheets together .for instance
ITEM FR-1 in sheets STA,RPA contain COST TOTAL =3000+1200+770=4370 IN column COST TOTAL into listbox
as to SALES TOTAL should merge for each two sheets together for instance
ITEM FR-5 in sheets STA,SR contain SALES TOTAL =30+40+15=85
as to the last column in listbox should subtract COST TOTAL from SALES TOTAL
so every sheet will show qty and merge duplicates items based on column B when run the userform . should cancel unit price average as the code does it .
also there is combobox1 contains sheets names . then should also apply your code when select specific sheet as code does
RESULT
the code
thanks
the orginal thread got help from @DanteAmore.combining data in listbox on userform across multiple sheets and calculate values amongst them
I hope from him or anybody can help.
the orginal code will calculation the stock across sheets with merge duplicate items for each sheet if they repeat in the same sheet based on column B .so what I want ignore columns unit price whether cost or sale . instead of that I want show COST TOTAL & SALES TOTAL but should merge for MULTIPLE sheets together .for instance
ITEM FR-1 in sheets STA,RPA contain COST TOTAL =3000+1200+770=4370 IN column COST TOTAL into listbox
as to SALES TOTAL should merge for each two sheets together for instance
ITEM FR-5 in sheets STA,SR contain SALES TOTAL =30+40+15=85
as to the last column in listbox should subtract COST TOTAL from SALES TOTAL
so every sheet will show qty and merge duplicates items based on column B when run the userform . should cancel unit price average as the code does it .
also there is combobox1 contains sheets names . then should also apply your code when select specific sheet as code does
kl.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 | TRY 12.00 | TRY 15.00 | TRY 2,400.00 | TRY 3,000.00 | ||
3 | 2 | FR-2 | FR | APPLE | LL | 100 | TRY 11.00 | TRY 17.00 | TRY 1,100.00 | TRY 1,700.00 | ||
4 | 3 | FR-3 | FR | PEAR | NN | 60 | TRY 12.00 | TRY 15.00 | TRY 720.00 | TRY 900.00 | ||
5 | 4 | FR-4 | FR | BANANA | 55 | TRY 13.00 | TRY 17.00 | TRY 715.00 | TRY 935.00 | |||
6 | 5 | VEG1 | VEG | TOMATO | SS | 50 | TRY 14.00 | TRY 16.00 | TRY 700.00 | TRY 800.00 | ||
7 | 6 | VEG2 | VEG | TOMATO | AA | 50 | TRY 11.00 | TRY 15.00 | TRY 550.00 | TRY 750.00 | ||
8 | 7 | FR-5 | FR1 | PEAR | MM | 1 | TRY 11.00 | TRY 15.00 | TRY 11.00 | TRY 15.00 | ||
STA |
Cell Formulas | ||
---|---|---|
Range | Formula | |
I2:I8 | I2 | =G2*F2 |
J2:J8 | J2 | =H2*F2 |
kl.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | UNIT COST | COST TOTAL | ||
2 | 1/1/2021 | FR-1 | FR | BANANA | TT | 100.00 | TRY 12.00 | TRY 1,200.00 | ||
3 | 1/2/2021 | FR-2 | FR | APPLE | LL | 50.00 | TRY 11.00 | TRY 550.00 | ||
4 | 1/3/2021 | FR-3 | FR | PEAR | NN | 60.00 | TRY 12.00 | TRY 720.00 | ||
5 | 1/4/2021 | FR-4 | FR | BANANA | 60.00 | TRY 13.00 | TRY 780.00 | |||
6 | 1/5/2021 | VEG1 | VEG | TOMATO | SS | 65.00 | TRY 14.00 | TRY 910.00 | ||
7 | 1/6/2021 | VEG2 | VEG | TOMATO | AA | 40.00 | TRY 11.00 | TRY 440.00 | ||
8 | 1/7/2021 | FR-11 | FR | BANANA | TT | 100.00 | TRY 12.00 | TRY 1,200.00 | ||
9 | 1/8/2021 | FR-1 | FR | BANANA | TT | 55.00 | TRY 14.00 | TRY 770.00 | ||
RPA |
kl.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | UNIT SALE | SALES TOTAL | ||
2 | 2/1/2021 | FR-1 | FR | BANANA | TT | 5.00 | TRY 15.00 | TRY 75.00 | ||
3 | 2/3/2021 | FR-3 | FR | PEAR | NN | 5.00 | TRY 15.00 | TRY 75.00 | ||
4 | 2/4/2021 | FR-4 | FR | BANANA | 2.00 | TRY 17.00 | TRY 34.00 | |||
5 | 2/5/2021 | VEG1 | VEG | TOMATO | SS | 3.00 | TRY 16.00 | TRY 48.00 | ||
6 | 2/6/2021 | VEG2 | VEG | TOMATO | AA | 4.00 | TRY 15.00 | TRY 60.00 | ||
7 | 2/8/2021 | FR-5 | FR1 | PEAR | MM | 2.00 | TRY 15.00 | TRY 30.00 | ||
8 | 2/8/2021 | FR-5 | FR1 | PEAR | MM | 2.00 | TRY 20.00 | TRY 40.00 | ||
SR |
kl.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | PRICE | TOTAL | ||
2 | 3/1/2021 | FR-1 | FR | BANANA | TT | 2.00 | TRY 15.00 | TRY 30.00 | ||
3 | 3/2/2021 | FR-3 | FR | PEAR | NN | 2.00 | TRY 15.00 | TRY 30.00 | ||
4 | 3/3/2021 | FR-1 | FR | BANANA | TT | 2.00 | TRY 15.00 | TRY 30.00 | ||
5 | 3/4/2021 | FR-3 | FR | PEAR | NN | 2.00 | TRY 15.00 | TRY 30.00 | ||
RR |
kl.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | DATE | ID | BR | TY | OR | QTY | PRICE | TOTAL | ||
2 | 1/1/2021 | FR-1 | FR | BANANA | TT | 5.00 | TRY 12.00 | TRY 60.00 | ||
3 | 1/2/2021 | FR-2 | FR | APPLE | LL | 10.00 | TRY 11.00 | TRY 110.00 | ||
4 | 1/3/2021 | FR-1 | FR | BANANA | TT | 15.00 | TRY 12.00 | TRY 180.00 | ||
5 | 1/4/2021 | FR-2 | FR | APPLE | LL | 20.00 | TRY 11.00 | TRY 220.00 | ||
SS |
RESULT
the code
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.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.Clear
ListBox1.List = Sheets(.Value).Range("A2", Sheets(.Value).Range("H" & Rows.Count).End(3)).Value
End With
End Sub
Private Sub UserForm_Activate()
Call LoadListbox
End Sub
Last edited: