Hi all,
I am using code below to get data from sheet into listbox:
Unfortunately I have empty row header. Do you have an idea how this could be changed so row header is Sheet Database Range A1:O1 ?
If it is not possible with .AddItem to do it, could someone translate this into RowSource ? - I have no idea how to do it with loop...
Thank you !
I am using code below to get data from sheet into listbox:
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
lstDatabase.ColumnHeads = True
lstDatabase.ColumnWidths = "10,15,55,55,60,60,45,55,55,55,55,55,55"
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
Unfortunately I have empty row header. Do you have an idea how this could be changed so row header is Sheet Database Range A1:O1 ?
If it is not possible with .AddItem to do it, could someone translate this into RowSource ? - I have no idea how to do it with loop...
Thank you !