collection showed data in listbox with sheet data and show in another listbox

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
193
Office Version
  1. 2019
Platform
  1. Windows
Hello ,
I have code to show data in listbox1 like this
mkl.PNG

I have data in sheet
mk (1).xlsm
ABC
1ITEMSAFESBALANCES
21ASDFT SAFE300,000.00
32YOUSEF SAFE150,000.00
43AFORI BANK250,000.00
54AMTOR BANK15,000.00
65ASTORI BANK100,000.00
76AMM SAFE280,000.00
87HBS BANK100,000.00
9TOTAL1,195,000.00
BAB
Cell Formulas
RangeFormula
C9C9=SUM(C2:C8)


and I add listbox2 , so what I want merge data amounts in PAID,RECEIVED columns based on items are existed in SAFES column for listbox1 ,as to amounts in column C for BAB sheet should sum with amounts for RECEIVED column and show result in listbox2 like this picture,
sd.JPG

also should add BALANCE column to subtract RECEIVED column from PAID column and add TOTAL row to sum RECEIVED,PAID columns and in BALANCE column subtract RECEIVED column from ,PAID column.
there are items are existed in BAB sheet and they are not existed in listbox1 so should show in listbox2 based on BAB sheet.
when show result in listbox2 I expect the showed data in listbox1 could be 15000 rows.
thanks
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
The data in the BAB sheet is not ordered by the SAFES column, however, in the listbox it does appear ordered by SAFES. So to achieve that, I need to copy the data from columns A:C and paste into columns AA:AC and sort the data by SAFES. Ultimately the data in columns AA:AC will be deleted.

Use the following code to load listbox2:
VBA Code:
Private Sub CommandButton2_Click()
  Dim a As Variant, b As Variant, c As Variant
  Dim dic As Object
  Dim i As Long, lr As Long, nRow As Long
  Dim dRec As Double, dPai As Double
  
  Set dic = CreateObject("Scripting.Dictionary")
  
  Application.ScreenUpdating = False
  With Sheets("BAB")
    lr = Sheets("BAB").Range("A" & Rows.Count).End(3).Row - 1
    .Range("A1:C" & lr).Copy .Range("AA1")
    .Range("AB1:AC" & lr).Sort .Range("AB1"), xlAscending, Header:=xlYes
    a = .Range("AA1:AE" & lr + 1).Value
    .Range("AA:AC").Clear
  End With
  Application.ScreenUpdating = True
  
  For i = 2 To UBound(a)
    dic(a(i, 2)) = i
  Next
  
  c = a
  b = ListBox1.List
  For i = 1 To UBound(b)
    If dic.exists(b(i, 2)) Then
      nRow = dic(b(i, 2))
      c(nRow, 3) = c(nRow, 3) + CDbl(Replace(b(i, 5), "-", 0))
      c(nRow, 4) = c(nRow, 4) + CDbl(Replace(b(i, 3), "-", 0))
      c(nRow, 5) = c(nRow, 3) - c(nRow, 4)
    End If
  Next
  
  For i = 2 To UBound(c)
    dRec = dRec + c(i, 3)
    dPai = dPai + c(i, 4)
    c(i, 3) = Format(Val(c(i, 3)), "#,##0.00;;-")
    c(i, 4) = Format(Val(c(i, 4)), "#,##0.00;;-")
    c(i, 5) = Format(Val(c(i, 5)), "#,##0.00;;-")
  Next
  c(1, 1) = "ITEM"
  c(1, 2) = "SAFES"
  c(1, 3) = "RECEIVED"
  c(1, 4) = "PAID"
  c(1, 5) = "BALANCE"
  c(UBound(c), 1) = "TOTAL"
  c(UBound(c), 3) = Format(dRec, "#,##0.00;;-")
  c(UBound(c), 4) = Format(dPai, "#,##0.00;;-")
  c(UBound(c), 5) = Format(dRec - dPai, "#,##0.00;;-")
  
  With ListBox2
    .ColumnCount = 5
    .List = c
  End With
End Sub

😇
 
Upvote 0
Solution

Forum statistics

Threads
1,225,897
Messages
6,187,708
Members
453,435
Latest member
U4US

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