adjusting code populate data in listbox based on selected sheet

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
985
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Watch MrExcel Video

Forum statistics

Threads
1,127,643
Messages
5,626,055
Members
416,158
Latest member
CaliburBlade138

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
Top