modification code to calculation stock on userform

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
354
Office Version
  1. 2016
Platform
  1. Windows
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
kl.xlsm
ABCDEFGHIJ
1ITEMIDBRTYORQTYUNIT COSTUNIT SALECOST TOTALSALES TOTAL
21FR-1FRBANANATT200TRY 12.00TRY 15.00TRY 2,400.00TRY 3,000.00
32FR-2FRAPPLELL100TRY 11.00TRY 17.00TRY 1,100.00TRY 1,700.00
43FR-3FRPEARNN60TRY 12.00TRY 15.00TRY 720.00TRY 900.00
54FR-4FRBANANAQQ55TRY 13.00TRY 17.00TRY 715.00TRY 935.00
65VEG1VEGTOMATOSS50TRY 14.00TRY 16.00TRY 700.00TRY 800.00
76VEG2VEGTOMATOAA50TRY 11.00TRY 15.00TRY 550.00TRY 750.00
87FR-5FR1PEARMM1TRY 11.00TRY 15.00TRY 11.00TRY 15.00
STA
Cell Formulas
RangeFormula
I2:I8I2=G2*F2
J2:J8J2=H2*F2



kl.xlsm
ABCDEFGH
1DATEIDBRTYORQTYUNIT COSTCOST TOTAL
21/1/2021FR-1FRBANANATT100.00TRY 12.00TRY 1,200.00
31/2/2021FR-2FRAPPLELL50.00TRY 11.00TRY 550.00
41/3/2021FR-3FRPEARNN60.00TRY 12.00TRY 720.00
51/4/2021FR-4FRBANANAQQ60.00TRY 13.00TRY 780.00
61/5/2021VEG1VEGTOMATOSS65.00TRY 14.00TRY 910.00
71/6/2021VEG2VEGTOMATOAA40.00TRY 11.00TRY 440.00
81/7/2021FR-11FRBANANATT100.00TRY 12.00TRY 1,200.00
91/8/2021FR-1FRBANANATT55.00TRY 14.00TRY 770.00
RPA



kl.xlsm
ABCDEFGH
1DATEIDBRTYORQTYUNIT SALESALES TOTAL
22/1/2021FR-1FRBANANATT5.00TRY 15.00TRY 75.00
32/3/2021FR-3FRPEARNN5.00TRY 15.00TRY 75.00
42/4/2021FR-4FRBANANAQQ2.00TRY 17.00TRY 34.00
52/5/2021VEG1VEGTOMATOSS3.00TRY 16.00TRY 48.00
62/6/2021VEG2VEGTOMATOAA4.00TRY 15.00TRY 60.00
72/8/2021FR-5FR1PEARMM2.00TRY 15.00TRY 30.00
82/8/2021FR-5FR1PEARMM2.00TRY 20.00TRY 40.00
SR


kl.xlsm
ABCDEFGH
1DATEIDBRTYORQTYPRICETOTAL
23/1/2021FR-1FRBANANATT2.00TRY 15.00TRY 30.00
33/2/2021FR-3FRPEARNN2.00TRY 15.00TRY 30.00
43/3/2021FR-1FRBANANATT2.00TRY 15.00TRY 30.00
53/4/2021FR-3FRPEARNN2.00TRY 15.00TRY 30.00
RR


kl.xlsm
ABCDEFGH
1DATEIDBRTYORQTYPRICETOTAL
21/1/2021FR-1FRBANANATT5.00TRY 12.00TRY 60.00
31/2/2021FR-2FRAPPLELL10.00TRY 11.00TRY 110.00
41/3/2021FR-1FRBANANATT15.00TRY 12.00TRY 180.00
51/4/2021FR-2FRAPPLELL20.00TRY 11.00TRY 220.00
SS


RESULT
KL.PNG


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
thanks
 
Last edited:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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