combining data in listbox on userform across multiple sheets and calculate values amongst them

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
354
Office Version
  1. 2016
Platform
  1. Windows
hi experts
I have many sheets about five sheets contains data are almost 3000 rows for each sheet and it will increase continuiosly . 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 prevouis year. the others sheets are currnt year operations with considering the second sheet somtimes contains new item then should show in listbox . after merge duplicate items should show the QTY for each sheet . about COL 11 the calculate like this as item (FR1)=200+200-5+4-20=379 . as to COLS 12,13 (UNIT COST,UNIT SALES ) should not summing . should keep as it is .
as to COL 14 should calculate like this (15-12)*379=1137
see the row1 in listbox
so if any body have suggestion to do that by using helper sheet or doing directly without using helper sheet I accept all the suggestion .

COLLECTION (2).xlsm
ABCDEFGH
1ITEMIDBRTYORQTYUNIT COSTUNIT SALE
21FR-1FRBANANATT200.00$12.00$15.00
32FR-2FRAPPLELL100.00$11.00$17.00
43FR-3FRPEARNN60.00$12.00$15.00
54FR-4FRBANANAQQ55.00$13.00$17.00
65VEG1VEGTOMATOSS50.00$14.00$16.00
76VEG2VEGTOMATOAA50.00$11.00$15.00
86FR-5FR1PEARMM0.00$11.00$15.00
STA



COLLECTION (2).xlsm
ABCDEFGH
1DATEIDBRTYORQTYUNIT COST TOTAL
21/1/2021FR-1FRBANANATT100.00$12.00$1,200.00
31/2/2021FR-2FRAPPLELL50.00$11.00$550.00
41/3/2021FR-3FRPEARNN60.00$12.00$720.00
51/4/2021FR-4FRBANANAQQ60.00$13.00$780.00
61/5/2021VEG1VEGTOMATOSS65.00$14.00$910.00
71/6/2021VEG2VEGTOMATOAA40.00$11.00$440.00
81/7/2021FR-1FRBANANATT100.00$12.00$1,200.00
91/8/2021FR-5FR1PEARMM55.00$14.00$770.00
RPA
Cell Formulas
RangeFormula
H2:H9H2=G2*F2




COLLECTION (2).xlsm
ABCDEFGH
1DATEIDBRTYORQTYUNIT SALETOTAL
22/1/2021FR-1FRBANANATT5.00$15.00$75.00
32/3/2021FR-3FRPEARNN5.00$15.00$75.00
42/4/2021FR-4FRBANANAQQ2.00$17.00$34.00
52/5/2021VEG1VEGTOMATOSS3.00$16.00$48.00
62/6/2021VEG2VEGTOMATOAA4.00$15.00$60.00
72/8/2021FR-5FR1PEARMM2.00$15.00$30.00
82/8/2021FR-5FR1PEARMM2.00$20.00$40.00
SR
Cell Formulas
RangeFormula
H2:H8H2=G2*F2


COLLECTION (2).xlsm
ABCDEFGH
1DATEIDBRTYORQTYPRICETOTAL
23/1/2021FR-1FRBANANATT2.00$15.00$30.00
33/2/2021FR-3FRPEARNN2.00$15.00$30.00
43/3/2021FR-1FRBANANATT2.00$15.00$30.00
53/4/2021FR-3FRPEARNN2.00$15.00$30.00
RR
Cell Formulas
RangeFormula
H2:H5H2=G2*F2


COLLECTION (2).xlsm
ABCDEFGH
1DATEIDBRTYORQTYPRICETOTAL
21/1/2021FR-1FRBANANATT5.00$12.00$60.00
31/2/2021FR-2FRAPPLELL10.00$11.00$110.00
41/3/2021FR-1FRBANANATT15.00$12.00$180.00
51/4/2021FR-2FRAPPLELL20.00$11.00$220.00
SS
Cell Formulas
RangeFormula
H2:H5H2=G2*F2



the result should be in listbox

1.PNG



2.PNG
thanks in advance
 
second this is my big mistake some times the prices changes .so I would take prices average just in sheet (STA,RPA,SA)
You need to explain it with the examples.

when take price average in COL12 in listbox should calculate the cost price average based on first sheet ,second(COL G)
The same, you must explain it with examples, what data do you have, what data do you expect as a result.

COL13 in listbox should calculate the sale price average based on first sheet ,third (COLS G,H)
The same, you must explain it with examples

finally I know this doesn't mentioned in OP if you did it I truly appreciate. I add combobox1 contains sheets names . then should also apply your code when select specific sheet as code does when run userform
The code must run when you select a sheet in the combo. Does this sheet have any relevance to the code?
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
thanks dante for feedback again . ok I' ll try answering your question. and attach some pictures to expected result . just give some time to do that .
thanks again ;)
 
Upvote 0
COLLECTION (2).xlsm
ABCDEFGH
1ITEMIDBRTYORQTYUNIT COSTUNIT SALE
21FR-1FRBANANATT200.00$13.00$15.00
32FR-2FRAPPLELL100.00$11.00$17.00
43FR-3FRPEARNN60.00$12.00$15.00
54FR-4FRBANANAQQ55.00$13.00$17.00
65VEG1VEGTOMATOSS50.00$14.00$16.00
76VEG2VEGTOMATOAA50.00$11.00$15.00
87FR-5FR1PEARMM0.00$11.00$15.00
98FR-6FR1PEARMM$12.00$15.00
STA



COLLECTION (2).xlsm
ABCDEFGH
1DATEIDBRTYORQTYUNIT COST TOTAL
21/1/2021FR-1FRBANANATT100.00$12.00$1,200.00
31/2/2021FR-2FRAPPLELL50.00$11.00$550.00
41/3/2021FR-3FRPEARNN60.00$12.00$720.00
51/4/2021FR-4FRBANANAQQ60.00$13.00$780.00
61/5/2021VEG1VEGTOMATOSS65.00$14.00$910.00
71/6/2021VEG2VEGTOMATOAA40.00$11.00$440.00
81/7/2021FR-1FRBANANATT100.00$14.00$1,400.00
91/8/2021FR-5FR1PEARMM55.00$14.00$770.00
101/9/2021FR-6FR1PEARMM56.00$15.00$840.00
RPA
Cell Formulas
RangeFormula
H2:H10H2=G2*F2




COLLECTION (2).xlsm
ABCDEFGH
1DATEIDBRTYORQTYUNIT SALETOTAL
22/1/2021FR-1FRBANANATT5.00$16.00$80.00
32/3/2021FR-3FRPEARNN5.00$15.00$75.00
42/4/2021FR-4FRBANANAQQ2.00$17.00$34.00
52/5/2021VEG1VEGTOMATOSS3.00$16.00$48.00
62/6/2021VEG2VEGTOMATOAA4.00$15.00$60.00
72/8/2021FR-5FR1PEARMM2.00$15.00$30.00
82/8/2021FR-5FR1PEARMM2.00$20.00$40.00
SR
Cell Formulas
RangeFormula
H2:H8H2=G2*F2

ok see attached pictures of the sheets(STA,RPA,SR) which depend on calculation prices cost, sale average so see the highlighted yeallow cells(ID) and red (cost,sale price) so lets take FR1 for instance price cost average = (12+13+14)/3=13 and the price sale average( 15+16)/2=15.5 as I said sales prices take from sheets STA, SR and cost price take from STA,RPA
see in listbox in first row and lastrow how show prices average in columns 12,13
1.PNG

The code must run when you select a sheet in the combo. Does this sheet have any relevance to the code?
if you mean this is not relating to the code and this is differnt thread I will issue a new thread. it's up to you . any way see attached the picture .
as the picture when select the specific sheet then should show the same data as in existed in sheet without merge or calculate average
and if the combobox is empty then should apply your code as when run the userform in first time
2.PNG
 
Upvote 0
Replace your code with the following:

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

Private Sub UserForm_Initialize()
  ComboBox1.AddItem "RPA"
End Sub
 
Upvote 0
Remove this part of the code, it was just for my test.
VBA Code:
Private Sub UserForm_Initialize()
  ComboBox1.AddItem "RPA"
End Sub
 
Upvote 0
that's awesome ! actually you misunderstood about add just one sheet , but I can tweak . I want adding all the sheets
then should be like this
VBA Code:
Private Sub UserForm_Initialize()
with combobox1
.additem "RPA"
.additem "SS"
.additem "SR"
.additem "RR"
.additem "SS"
end with

 
End Sub
it remains one thing when select sheet it doesn't show numbers formatting and currency . can you fix it ,please ?
 
Upvote 0
actually you misunderstood about add just one sheet
That was not in your initial request.
I am trying to adjust it. It is a fairly extensive code with multiple requirements.

it remains one thing when select sheet it doesn't show numbers formatting and currency . can you fix it ,please ?
Try the following code, but the format in the listbox will take the format of the cells of the selected sheet themselves. So if you don't put the format in the cells, then the format won't appear in the listbox either. But if you put it in the cell, that will take the macro.


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
 
Upvote 0
Solution
I am trying to adjust it. It is a fairly extensive code with multiple requirements.
I appreciate that . now the code works as what I want , but there is problem after select sheet and select again another sheet . it shows unspecified error
VBA Code:
 ListBox1.Clear
 
Upvote 0
now the code works as what I want , but there is problem after select sheet and select again another sheet . it shows unspecified error
Copy all the code from the #18 post again, I already did that update.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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