abdelfattah
Well-known Member
- Joined
- May 3, 2019
- Messages
- 1,430
- Office Version
- 2019
- 2010
- Platform
- Windows
hi, expert
I have this code populate data for all sheets in listbox I would amend it when I select specific sheet by combobox
Private Sub UserForm_Initialize()
Dim s As Long
For s = 1 To Sheets.Count
ComboBox1.AddItem Sheets(s).Name
Next
Dim ws As Worksheet, a, i As Long, j As Long
ListBox1.ColumnWidths = "100;85;85;80;50" 'COLUMN WIdTH IN LISTBOX
ListBox1.ColumnCount = 4 'COLUMN NUMBER OF LISTBOX
'loop through worksheets
j = 0 'Listbox item count
For Each ws In Worksheets
'use the code name in case sheet name changes
Select Case ws.CodeName
'exclude these sheets by code name
Case "Sheet1"
'Add the rest
Case Else
a = ws.Range("A3:D" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
With ListBox1
For i = 1 To UBound(a)
.AddItem
.List(j, 0) = (a(i, 1))
.List(j, 1) = (a(i, 2))
.List(j, 2) = (a(i, 3))
.List(j, 3) = (a(i, 4))
j = j + 1
Next i
End With
End Select
Next ws
End Sub
I have this code populate data for all sheets in listbox I would amend it when I select specific sheet by combobox
Private Sub UserForm_Initialize()
Dim s As Long
For s = 1 To Sheets.Count
ComboBox1.AddItem Sheets(s).Name
Next
Dim ws As Worksheet, a, i As Long, j As Long
ListBox1.ColumnWidths = "100;85;85;80;50" 'COLUMN WIdTH IN LISTBOX
ListBox1.ColumnCount = 4 'COLUMN NUMBER OF LISTBOX
'loop through worksheets
j = 0 'Listbox item count
For Each ws In Worksheets
'use the code name in case sheet name changes
Select Case ws.CodeName
'exclude these sheets by code name
Case "Sheet1"
'Add the rest
Case Else
a = ws.Range("A3:D" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
With ListBox1
For i = 1 To UBound(a)
.AddItem
.List(j, 0) = (a(i, 1))
.List(j, 1) = (a(i, 2))
.List(j, 2) = (a(i, 3))
.List(j, 3) = (a(i, 4))
j = j + 1
Next i
End With
End Select
Next ws
End Sub