combine data in listbox on userform based on another listbox

Maklil

Board Regular
Joined
Jun 23, 2022
Messages
181
Office Version
  1. 2019
Platform
  1. Windows
Hello
I would show data across sheets by arrange for each name and each sheet and should show data in listbox start from first sheet and sort from first date to last date .
mk1.xlsm
ABCDEFGH
1DATEINV NONAMEIDCASEQTYUNIT PRICEBALANCE
220/08/2023MMUY7000MVSOOIL AS-100PAID20.00160.003,200.00
320/08/2023MMUY7000MVSOOIL AS-101PAID20.00180.003,600.00
4TOTAL6,800.00
520/08/2023MMUY7001MVSOOIL AS-102PAID40.00155.006,200.00
620/08/2023MMUY7001MVSOOIL AS-103PAID20.00190.003,800.00
7TOTAL10,000.00
821/08/2023MMUY7002MVSOOIL AS-103NOT PAID25.00190.004,750.00
9TOTAL4,750.00
1021/08/2023MSSOOIL AS-103NOT PAID25.00180.004,500.00
1121/08/2023MSSOOIL AS-104NOT PAID40.00177.007,080.00
12TOTALOOIL AS-10511,580.00
1321/08/2023MMUY7002MVSOOIL AS-103NOT PAID100.00196.0019,600.00
14TOTAL19,600.00
15
MK
Cell Formulas
RangeFormula
H13,H10:H11,H8,H5:H6,H2:H3H2=F2*G2
H4,H12,H7H4=SUM(H2:H3)
H9,H14H9=SUM(H8)




mk1.xlsm
ABCDEFGH
1DATEINV NONAMEIDCASEQTYUNIT PRICEBALANCE
220/08/2023ST NO 1000MVSGNOO HH 1200RECEIVED10.00150.001,500.00
320/08/2023ST NO 1000MVSAS100-12RECEIVED10.00130.001,300.00
4TOTAL2,800.00
521/08/2023ST NO 1001MTTMGFH GA-103NOT REICEVED15.00130.001,950.00
6TOTAL1,950.00
721/08/2023ST NO 1002MLLSSFOO 1000 MN1NOT REICEVED2.00140.00280.00
821/08/2023ST NO 1002MLLSSFOO 1000 MN2NOT REICEVED12.00145.001,740.00
921/08/2023ST NO 1002MLLSSFOO 1000 MN3NOT REICEVED10.00145.001,450.00
10TOTAL3,470.00
1124/08/2023VT NO 1003MKKOOIL AS-100RECEIVED10.00145.001,450.00
1224/08/2023VT NO 1003MKKOOIL AS-101RECEIVED25.00150.003,750.00
1324/08/2023VT NO 1003MKKOOIL AS-102RECEIVED40.00155.006,200.00
1424/08/2023VT NO 1003MKKOOIL AS-103RECEIVED55.00160.008,800.00
1524/08/2023VT NO 1003MKKOOIL AS-104RECEIVED70.00165.0011,550.00
1624/08/2023VT NO 1003MKKOOIL AS-105RECEIVED85.00170.0014,450.00
1724/08/2023VT NO 1003MKKOOIL AS-106RECEIVED100.00175.0017,500.00
1824/08/2023VT NO 1003MKKOOIL AS-107RECEIVED115.00180.0020,700.00
19TOTAL84,400.00
2024/08/2023ST NO 1002MLLSSFOO 1000 MN11NOT REICEVED12.00160.001,920.00
2124/08/2023ST NO 1002MLLSSFOO 1000 MN21NOT REICEVED12.00170.002,040.00
22TOTAL3,960.00
MT
Cell Formulas
RangeFormula
H11:H18,H5,H2:H3H2=F2*G2
H4,H22H4=SUM(H2:H3)
H6H6=SUM(H5:H5)
H20:H21,H7:H9H7=G7*F7
H10H10=SUM(H7:H9)
H19H19=SUM(H11:H18)




mk1.xlsm
ABCDEFGH
1DATEINV NONAMEIDCASEQTYUNIT PRICEBALANCE
220/08/2023MSSUY4000MVSOOIL AS-100RECEIVED1.00160.00160.00
320/08/2023MSSUY4000MVSOOIL AS-101RECEIVED1.00180.00180.00
4TOTAL340.00
520/08/2023MSSUY4001MVSOOIL AS-102NOT REICEVED1.00155.00155.00
620/08/2023MSSUY4001MVSOOIL AS-103NOT REICEVED1.00190.00190.00
7TOTAL345.00
821/08/2023MSSUY4002MVSOOIL AS-102RECEIVED2.00155.00310.00
9TOTAL655.00
1021/08/2023MSSUY4003MSSOOIL AS-100NOT REICEVED1.00160.00160.00
1121/08/2023MSSUY4003MSSOOIL AS-101NOT REICEVED1.00180.00180.00
1221/08/2023MSSUY4003MSSOOIL AS-102NOT REICEVED1.00155.00155.00
13TOTAL495.00
MS
Cell Formulas
RangeFormula
H10:H12,H8,H5:H6,H2:H3H2=F2*G2
H4,H9,H7H4=SUM(H2:H3)
H13H13=SUM(H10:H12)



mk1.xlsm
ABCDEFGH
1DATEINV NONAMEIDCASEQTYUNIT PRICEBALANCE
224/08/2023VT NO 1003MKKOOIL AS-100NOT PAID1.00145.00145.00
324/08/2023VT NO 1003MKKOOIL AS-101NOT PAID1.00150.00150.00
424/08/2023VT NO 1003MKKOOIL AS-102NOT PAID2.00155.00310.00
524/08/2023VT NO 1003MKKOOIL AS-103NOT PAID4.00160.00640.00
6TOTAL1,245.00
725/08/2023VT NO 1004MKKOOIL AS-101PAID1.00150.00150.00
825/08/2023VT NO 1004MKKOOIL AS-102PAID2.00155.00310.00
9TOTAL460.00
1025/08/2023VT NO 1005MLLOOIL AS-103PAID4.00160.00640.00
11TOTAL640.00
1225/08/2023VT NO 1006MLLOOIL AS-102PAID2.00155.00310.00
1325/08/2023VT NO 1006MLLOOIL AS-103PAID4.00160.00640.00
14TOTAL950.00
1525/08/2023VT NO 1007MVSOOIL AS-101PAID3.00150.00450.00
16TOTAL450.00
ATS
Cell Formulas
RangeFormula
H15,H12:H13,H10,H7:H8,H2:H5H2=F2*G2
H6H6=SUM(H2:H5)
H9,H14H9=SUM(H7:H8)
H11,H16H11=SUM(H10)



so in first column will show date dd/mm/yyyy
second column brings names
third column populate sheets names for each name where is location in sheet.
fourth second shows CASE column
fifth column will brings amount from TOTAL row for each name from column H .
after show data in listbox1 then will merge data in listbox2 based on sheet name and case columns for 3,4 together.
so the form before
as1.PNG



after
as2.PNG

and if it's possible also when write dates in TB1,TB2 so will sort in listbox1 based dates and merged in listbox2 .
if TB1,TB2 are empty then will show all of data across sheets.
the data could be 10000 rows for each sheet.
thanks.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try the following code in your userform.
It may be a little slow at first, as it will load all the data from all the sheets into an array, and the data will be placed in columns AA to AE in sheet "MK" for sorting, after that the columns will be cleared.

Filters with textbox1 and textbox2 should be faster.

Set the sheets and their names in this line of the macro:
arr = Array("MK", "MT", "MS", "ATS")​

VBA Code:
Option Explicit 'At the beginning of all the code

Dim c As Variant
Const frm As String = "#,##0.00"

Private Sub Textbox1_Change()
  Call FilterData
End Sub
Private Sub Textbox2_Change()
  Call FilterData
End Sub

Private Sub FilterData()
  Dim tb1 As Date, tb2 As Date
  Dim i As Long, j As Long, k As Long, y As Long, nRow As Long
  Dim b As Variant, d As Variant, ky As Variant
  Dim dic As Object
  
  If Len(TextBox1.Value) <> 10 And TextBox1 <> "" Then Exit Sub
  If Len(TextBox2.Value) <> 10 And TextBox2 <> "" Then Exit Sub
    
  Set dic = CreateObject("Scripting.Dictionary")
  ListBox1.Clear
  ListBox2.Clear
  ReDim b(1 To UBound(c, 1), 1 To UBound(c, 2))
  ReDim d(1 To UBound(c, 1), 1 To 4)
  k = 1
  b(k, 1) = "DATE"
  b(k, 2) = "NAME"
  b(k, 3) = "SHEETS NAMES"
  b(k, 4) = "CASE"
  b(k, 5) = "BALANCE"
  
  y = 1
  d(y, 1) = "ITEM"
  d(y, 2) = "SHEETS NAMES"
  d(y, 3) = "CASE"
  d(y, 4) = "BALANCE"
  
  For i = 2 To UBound(c)
    If TextBox1.Value = "" Then tb1 = CDate(c(i, 1)) Else tb1 = CDate(TextBox1.Value)
    If TextBox2.Value = "" Then tb2 = CDate(c(i, 1)) Else tb2 = CDate(TextBox2.Value)
    If CDate(c(i, 1)) >= tb1 And CDate(c(i, 1)) <= tb2 Then
      k = k + 1
      For j = 1 To UBound(c, 2)
        b(k, j) = c(i, j)
      Next
      
      ky = b(k, 3) & "|" & b(k, 4)
      If Not dic.exists(ky) Then
        y = y + 1
        dic(ky) = y
      End If
      nRow = dic(ky)
      d(nRow, 1) = nRow - 1
      d(nRow, 2) = b(k, 3)
      d(nRow, 3) = b(k, 4)
      If b(k, 5) <> "" Then
        d(nRow, 4) = Format(CDbl(d(nRow, 4)) + CDbl(b(k, 5)), frm) 'a(i, 8)
      End If

    End If
  Next
  
  If k = 0 Then
    MsgBox "doesn't show data"
  Else
    ListBox1.List = b
    ListBox2.List = d
  End If
End Sub

Private Sub UserForm_Activate()
  Dim i As Long, nMax As Long, k As Long, lr As Long
  Dim y As Long, nRow As Long
  Dim sh As Worksheet
  Dim arr As Variant, itm As Variant, ky As Variant
  Dim a As Variant, b As Variant, d As Variant
  Dim dic As Object
  
  arr = Array("MK", "MT", "MS", "ATS")
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each itm In arr
    Set sh = Sheets(itm)
    nMax = nMax + WorksheetFunction.CountIf(sh.Range("A:A"), "TOTAL")
  Next
  ReDim b(1 To nMax, 1 To 5)    'to listbox1
  ReDim d(1 To nMax, 1 To 4)    'to listbox2
  
  y = 1
  d(y, 1) = "ITEM"
  d(y, 2) = "SHEETS NAMES"
  d(y, 3) = "CASE"
  d(y, 4) = "BALANCE"

  For Each itm In arr
    Set sh = Sheets(itm)
    a = sh.Range("A2", sh.Range("H" & Rows.Count).End(3)).Value
    For i = 1 To UBound(a)
      If a(i, 1) = "TOTAL" Then
        k = k + 1
        b(k, 1) = Format(a(i - 1, 1), "dd/mm/yyyy")
        b(k, 2) = a(i - 1, 3)
        b(k, 3) = sh.Name
        b(k, 4) = a(i - 1, 5)
        b(k, 5) = Format(a(i, 8), frm)
        
        ky = b(k, 3) & "|" & b(k, 4)
        If Not dic.exists(ky) Then
          y = y + 1
          dic(ky) = y
        End If
        nRow = dic(ky)
        d(nRow, 1) = nRow - 1
        d(nRow, 2) = b(k, 3)
        d(nRow, 3) = b(k, 4)
        If b(k, 5) <> "" Then
          d(nRow, 4) = Format(CDbl(d(nRow, 4)) + CDbl(b(k, 5)), frm) 'a(i, 8)
        End If
      End If
    Next
  Next
  
  Application.ScreenUpdating = False
  With Sheets("MK")
    .Range("AA:AE").ClearContents
    .Range("AA:AE").NumberFormat = "@"
    .Range("AA1").Resize(1, 5).Value = Array("DATE", "NAME", "SHEETS NAMES", "CASE", "BALANCE")
    .Range("AA2").Resize(k, 5).Value = b
    lr = .Range("AA" & Rows.Count).End(3).Row
    .Range("AA1:AE" & lr).Sort .Range("AA1"), xlAscending, .Range("AB1"), , xlAscending, Header:=xlYes
    c = .Range("AA1:AE" & lr).Value
    .Range("AA:AE").ClearContents
  End With
  Application.ScreenUpdating = True
  
  With ListBox1
    .ColumnCount = 5
    .List = c
  End With
  With ListBox2
    .ColumnCount = 4
    .List = d
  End With
End Sub

😇
 
Upvote 0
Try the following code with improvements:


VBA Code:
Option Explicit 'At the beginning of all the code

Dim c As Variant

Private Sub Textbox1_Change()
  Call FilterData
End Sub
Private Sub Textbox2_Change()
  Call FilterData
End Sub

Private Sub FilterData()
  Dim tb1 As Date, tb2 As Date
  Dim i As Long, j As Long, k As Long, y As Long, nRow As Long
  Dim b As Variant, d As Variant, ky As Variant
  Dim dic As Object
 
  'Date validation
  If Len(TextBox1.Value) <> 10 And TextBox1 <> "" Then Exit Sub
  If Len(TextBox2.Value) <> 10 And TextBox2 <> "" Then Exit Sub
  
  'Prepare listbox
  Set dic = CreateObject("Scripting.Dictionary")
  ListBox1.Clear
  ListBox2.Clear
  ReDim b(1 To UBound(c, 1), 1 To UBound(c, 2))
  ReDim d(1 To UBound(c, 1), 1 To 4)
  k = 1
  b(k, 1) = "DATE"
  b(k, 2) = "NAME"
  b(k, 3) = "SHEETS NAMES"
  b(k, 4) = "CASE"
  b(k, 5) = "BALANCE"
 
  y = 1
  d(y, 1) = "ITEM"
  d(y, 2) = "SHEETS NAMES"
  d(y, 3) = "CASE"
  d(y, 4) = "BALANCE"
 
  'Data processing
  For i = 2 To UBound(c)
    If TextBox1.Value = "" Then tb1 = CDate(c(i, 1)) Else tb1 = CDate(TextBox1.Value)
    If TextBox2.Value = "" Then tb2 = CDate(c(i, 1)) Else tb2 = CDate(TextBox2.Value)
    If CDate(c(i, 1)) >= tb1 And CDate(c(i, 1)) <= tb2 Then
      k = k + 1
      For j = 1 To UBound(c, 2)
        b(k, j) = c(i, j)
      Next
     
      ky = b(k, 3) & "|" & b(k, 4)
      If Not dic.exists(ky) Then
        y = y + 1
        dic(ky) = y
      End If
      nRow = dic(ky)
      d(nRow, 1) = nRow - 1
      d(nRow, 2) = b(k, 3)
      d(nRow, 3) = b(k, 4)
      d(nRow, 4) = Format(CDbl(d(nRow, 4)) + CDbl(b(k, 5)), "#,##0.00")

    End If
  Next
 
  'Output
  ListBox1.List = b
  ListBox2.List = d
End Sub

Private Sub UserForm_Activate()
  Dim i As Long, nMax As Long, k As Long
  Dim sh As Worksheet
  Dim arr As Variant, itm As Variant
  Dim a As Variant, b As Variant
 
  arr = Array("MK", "MT", "MS", "ATS")      'fit sheets name
 
  For Each itm In arr
    Set sh = Sheets(itm)
    nMax = nMax + WorksheetFunction.CountIf(sh.Range("A:A"), "TOTAL")
  Next
  ReDim b(1 To nMax, 1 To 5)    'to listbox1
  ReDim d(1 To nMax, 1 To 4)    'to listbox2
 
  For Each itm In arr
    Set sh = Sheets(itm)
    a = sh.Range("A2", sh.Range("H" & Rows.Count).End(3)).Value
    For i = 1 To UBound(a)
      If a(i, 1) = "TOTAL" Then
        k = k + 1
        b(k, 1) = Format(a(i - 1, 1), "dd/mm/yyyy")
        b(k, 2) = a(i - 1, 3)
        b(k, 3) = sh.Name
        b(k, 4) = a(i - 1, 5)
        b(k, 5) = Format(a(i, 8), "#,##0.00")
      End If
    Next
  Next
 
  Application.ScreenUpdating = False
  With Sheets("MK")
    .Range("AA:AE").ClearContents
    .Range("AA:AE").NumberFormat = "@"
    .Range("AA2").Resize(k, 5).Value = b
    .Range("AA1:AE" & UBound(b) + 1).Sort .Range("AA1"), xlAscending, .Range("AB1"), , xlAscending, Header:=xlYes
    c = .Range("AA1:AE" & UBound(b) + 1).Value
    .Range("AA:AE").ClearContents
  End With
  Application.ScreenUpdating = True
 
  ListBox1.ColumnCount = 5
  ListBox2.ColumnCount = 4
 
  Call FilterData
End Sub

😇
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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