adjusting code populate data in listbox based on selected sheet

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
914
Office Version
  1. 2010
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
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Watch MrExcel Video

Forum statistics

Threads
1,118,861
Messages
5,574,709
Members
412,613
Latest member
EFRATA
Top