Populate combobox with sheets name

jayymehta

New Member
Joined
Jan 3, 2018
Messages
18
Hi, I have a Sub which selects an Excel file by dialog box. On selecting the file, a TextBox (Besides the Browse button in Userform) displays the full path of the selected file. Now There's also a Combobox below that textbox in which I want to populate all the Sheet names which are there in the selected file.
For ex, There is an excel file "Test.xlsm" in which are 3 sheets (Sheet1, Sheet2, Sheet3). So when "Test.xslm" is selected, the combobox should populate with "Sheet1", "Sheet2" and "Sheet3" names inside it. I tried making the code for above, but don't know how to deal with populating combobox with sheet names.

Code:
Private Sub CommandButton1_Click()  Dim fName As String
  Dim subfiles As Worksheets
  
  fName = Application.GetOpenFilename("Excel Files(*.xlsm),*.xlsm", , "Please select one **** excel file", , False)
  
  If fName = "" Then
    MsgBox "no file is selected.", vbExclamation, "Sorry!"
     Exit Sub
  Else
    TextBox1.Value = fName
  End If
  
  Set subfiles = fNames.Worksheet(i)
  
  For Each subfiles In fName
    .ComboBox1.AddItem = i
  Next i
    
End Sub

Kindly help me out of this.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
To load the sheet names into a ComboBox you would need a script like this.
But I do believe the workbook would have to be open and not just selected.

Code:
Private Sub CommandButton3_Click()
Dim i As Long
    For i = 1 To Sheets.Count
        ComboBox1.AddItem Sheets(i).Name
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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