Display data of Specific Sheets on Listbox Userform

sofas

Active Member
Joined
Sep 11, 2022
Messages
469
Office Version
  1. 2019
Platform
  1. Windows
Hello. After searching, I found this code to search and display data inside the listbox. I want to modify it or get a different code. It enables me to display the data of 3 Excel sheets from inside the workbook on the list. I tried this, but to no avail.





VBA Code:
Private Sub TextBox1_Change()
  
    Dim ws          As Worksheet
    Dim rng         As Range
    Dim r           As Long, c As Long, Lastrow As Long, r1 As Long
    Dim Search      As String
    Dim FilterArr()   As Variant
  
    Search = Me.TextBox1.Value
  
    Set ws = ThisWorkbook.Worksheets("Sheet2")
  
    Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
  
    'size worksheet data range
    Set rng = ws.Cells(2, 1).Resize(Lastrow, 28)
  
    arr = rng.Value2
  
    ReDim FilterArr(1 To rng.Rows.Count, 1 To rng.Columns.Count)
  
    With Me.ListBox1
        'disconnect rowsource
        .RowSource = ""
        .ColumnHeads = False
      
        'size listbox
        .ColumnCount = rng.Columns.Count
      
        .Clear
      
        If Len(Search) > 0 Then
          
            For r = 1 To UBound(arr, 1)
                If UCase(arr(r, 5)) Like UCase(Search) & "*" Then
                    r1 = r1 + 1
                    For c = 1 To UBound(arr, 2)
                        FilterArr(r1, c) = arr(r, c)
                    Next c
                End If
            Next r
          
            .List = ResizeArray(FilterArr, r1)
          
        Else
          
            'display full list
            .List = arr
          
            'or use rowsource
            're-connect rowsource to display all data with column heads
            .RowSource = ws.Name & "!" & rng.Address
            .ColumnHeads = True
          
        End If
      
    End With
  
End Sub
Function ResizeArray(ByVal arr As Variant, ByVal RowsCount As Long) As Variant
    Dim r       As Long, c As Long
    Dim Arr2()  As Variant
    
     If RowsCount > 0 Then
     'size array to match filtered data
        ReDim Arr2(1 To RowsCount, 1 To UBound(arr, 2))
        For r = 1 To RowsCount
            For c = 1 To UBound(arr, 2)
        'pass matching elements of arr to arr2
            Arr2(r, c) = arr(r, c)
            Next c
        Next
     End If
    ResizeArray = IIf(RowsCount > 0, Arr2, Array("No Match Found"))
End Function


Code:
Dim Rng As Range
Dim r As Long, c As Long, Lastrow As Long, r1 As Long
Dim FilterArr() As Variant
Dim wb As Workbook
Set wb = ThisWorkbook

Me.ListBox1.Clear
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    Select Case ws.Name

        Case "Sheet1", "Sheet2", "Sheet3"
        
        Lastrow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
             Set Rng = ws.Cells(2, 1).Resize(Lastrow, 11)
            

    arr = Rng.Value2
    
    ReDim FilterArr(1 To Rng.Rows.Count, 1 To Rng.Columns.Count)
    
    With Me.ListBox1
        .RowSource = ""
        .ColumnHeads = False
        .ColumnCount = Rng.Columns.Count
         For r = 1 To UBound(arr, 1)
                    r1 = r1 + 1
                    For c = 1 To UBound(arr, 2)
                        FilterArr(r1, c) = arr(r, c)
                    Next c
        
            Next r
            
            .List = ResizeArray(FilterArr, r1)
            
       .List = arr
       .RowSource = ws.Name & "!" & Rng.Address
        .ColumnHeads = True
        End With
        
 End Select
Next ws
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Good evening Is there any suggestion or idea that will enable me to get the desired result?
 
Upvote 0
Could you please upload a sample workbook (without sensitive data) to a file-sharing site like Dropbox.com or Google Drive, and then share the link here? Also, ensure that the link is accessible to anyone.
I found this code to search and display data inside the listbox.
To search in which column?
 
Upvote 0
Could you please upload a sample workbook (without sensitive data) to a file-sharing site like Dropbox.com or Google Drive, and then share the link here? Also, ensure that the link is accessible to anyone.

To search in which column?

Thank you for your interest. I tried to explain what is required within the file

want to search in sujets 1, 2 and 3
VBA Code:
 'If UCase(arr(r, 3)) Like UCase(Search) & "*" and UCase(arr(r, 11)) = Me.ComboBox2.Value & "*" And (arr(r, 2)) >= Me.ComboBox3.Value And (arr(r, 2)) <= Me.ComboBox4.Value Then


 
Upvote 0
Thank you for your interest. I tried to explain what is required within the file

want to search in sujets 1, 2 and 3
VBA Code:
 'If UCase(arr(r, 3)) Like UCase(Search) & "*" and UCase(arr(r, 11)) = Me.ComboBox2.Value & "*" And (arr(r, 2)) >= Me.ComboBox3.Value And (arr(r, 2)) <= Me.ComboBox4.Value Then


 
Upvote 0
I've downloaded your file. I'll try to write the code tomorrow .
 
Upvote 0
I've downloaded your file. I'll try to write the code tomorrow .

Merci encore. J’ai pu obtenir un échantillon de la forme souhaitée, mais il ne récupère que les données d’une seule table. Vous pouvez le vérifier pour rendre l’idée plus claire

 
Upvote 0
Merci encore. J’ai pu obtenir un échantillon de la forme souhaitée, mais il ne récupère que les données d’une seule table. Vous pouvez le vérifier pour rendre l’idée plus claire
Please use English when replying.

I've written some parts of the code, it's not finished yet because you want to use 4 criteria (using 4 comboboxes) to filter the data, so it's complicated.
I'll come back once I finish the code.
 
Upvote 0
@sofas
Sorry for the late reply.
As I said before, it's a bit complicated because of 4 criteria (using 4 comboboxes) to filter the data. And it's also complicated to filter the listbox base on date between 2 dates.
Try this:
sofas - TEST FILTRE DATA 1.xlsm
 
Upvote 0
@sofas
Sorry for the late reply.
As I said before, it's a bit complicated because of 4 criteria (using 4 comboboxes) to filter the data. And it's also complicated to filter the listbox base on date between 2 dates.
Try this:
sofas - TEST FILTRE DATA 1.xlsm
Thank you very much. It is actually a bit complicated. Sorry for the inconvenience. This is actually what I wanted to get. I'm sorry again.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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