Populate listbox row by row, depenadble of cell values

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
92
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi,

for example, there is a table A1:D10 (headers included).
In column D:D are values:
1. Logistics
2. Direct Purcashing

If I have login/logout for each user in excel separate sheet (like database), how I can make "If statement" for loop to populate listbox with data by D column?
The truth is I dont want to use two different sheets for this job. I would like to use one table as database.

Something like:
For Each i In Sheets("Database").Range("H3", Sheets("Database").Range("H" & Application.Rows.Count).End(xlUp))
If ..... Then
... .RowSource = ...
End If...

Next i

Is that possible?
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
If I understood you correctly this is what you're after.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
        ListBox1.List = Sheets("Sheet1").ListObjects("Table1").ListColumns(4).DataBodyRange.Value
End Sub
 

Attachments

  • 1673269729104.png
    1673269729104.png
    46.8 KB · Views: 6
Upvote 0
Um, not exactly.
here is more detailed:
1673270055746.png


Code:
iRow = WorksheetFunction.CountA(Sheets("Database").Range("A:A"))
.
.
.
With TaskManager
.lstDatabase.ColumnCount = 13
        .lstDatabase.ColumnHeads = True
        .lstDatabase.ColumnWidths = "10,15,55,55,60,60,45,55,55,55,55,55,55"
        If iRow > 1 Then
            .lstDatabase.RowSource = "Database" & "!A2:N" & iRow
        Else
            .lstDatabase.RowSource = "Database" & "!A2:N2"
        End If
End With

This is part of the code which I use now for row population in listbox. I would like to change it to be dependable of Area column (Column 3 in listbox). In separate sheet I have:
1673270357535.png

By this table I check cell B9 and then if there is written "Logistics" or "Direct Purchasing" I want to populate just these rows in listbox (comparing B9 to Database column 3 for example)
In department could be written both, so it has to be "Find", something like
Code:
If Instr(......)
 
Upvote 0
Found a way:
Code:
Private Sub UserForm_Initialize()
    Dim dataRange As Range
    Dim oneCell As Range
    Dim i As Long

    With Sheet8
        Set dataRange = Range(.Cells(Rows.Count, 1).End(xlUp), .Range("O1"))
    End With
    
    lstDatabase.ColumnCount = dataRange.Columns.Count - 1
    lstDatabase.List = dataRange.Resize(1, dataRange.Columns.Count - 1).Value
    
 If Sheet6.Range("B9") = "Logistics" Then
    For Each oneCell In dataRange.Columns(15).Cells
        If oneCell.Value = "Logistics" Then
            With oneCell.EntireRow
                lstDatabase.AddItem .Cells(1, 1).Value
                For i = 1 To lstDatabase.ColumnCount - 1
                    lstDatabase.List(lstDatabase.ListCount - 1, i) = .Cells(1, i + 1).Value
                Next i
            End With
        End If
    Next oneCell
 ElseIf Sheet6.Range("B9") = "Direct Purchasing" Then
    For Each oneCell In dataRange.Columns(15).Cells
        If oneCell.Value = "Direct Purchasing" Then
            With oneCell.EntireRow
                lstDatabase.AddItem .Cells(1, 1).Value
                For i = 1 To lstDatabase.ColumnCount - 1
                    lstDatabase.List(lstDatabase.ListCount - 1, i) = .Cells(1, i + 1).Value
                Next i
            End With
        End If
    Next oneCell
 ElseIf Sheet6.Range("B9") = "Foreign Trade" Then
    For Each oneCell In dataRange.Columns(15).Cells
        If oneCell.Value = "Foreign Trade" Then
            With oneCell.EntireRow
                lstDatabase.AddItem .Cells(1, 1).Value
                For i = 1 To lstDatabase.ColumnCount - 1
                    lstDatabase.List(lstDatabase.ListCount - 1, i) = .Cells(1, i + 1).Value
                Next i
            End With
        End If
    Next oneCell
 Else
     For Each oneCell In dataRange.Columns(15).Cells
            With oneCell.EntireRow
                lstDatabase.AddItem .Cells(1, 1).Value
                For i = 1 To lstDatabase.ColumnCount - 1
                    lstDatabase.List(lstDatabase.ListCount - 1, i) = .Cells(1, i + 1).Value
                Next i
            End With
    Next oneCell
End If
    lstDatabase.RemoveItem 0
End Sub

This code looks for dataRange in column O:O, afterwards it take values from that column and compare data which will be shown in the listbox named lstDatabase.
Good luck if anyone needs this code :)
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,366
Members
449,080
Latest member
Armadillos

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