adjusting code populate data in listbox based on selected sheet

abdelfattah

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

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,215,523
Messages
6,125,320
Members
449,218
Latest member
Excel Master

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