Cycling through worksheets in a different workbook

ccordner

Active Member
Joined
Apr 28, 2010
Messages
355
Hi

I want to get some data from a workbook, but it could be saved anywhere, under any name and the sheet I want might not always have the same name.

So I've built a userform with a combobox, in the hope that people can choose the worksheet they want.

Any ideas what's wrong with this code?

Code:
FileName = Application.GetOpenFilename("Microsoft Excel Spreadsheet (*.xlsx),*.xlsx," & _
                                        "Microsoft Excel Macro-Enabled Workbook (*.xlsm),*.xlsm," & _
                                        "Microsoft Excel 97-2003 Spreadsheet (*.xls),*.xls," & _
                                        "View All Files (*.*),*.*,", 1)
    
OldFileName = FileName
Workbooks.Open FileName
FileName = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))
Load frmSelectWorksheet
For Each WSheet In FileName
    frmSelectWorksheet.cmbWorkSheet.AddItem Worksheet.Name
Next

Thanks
Chris
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe something like this...

Code:
[font=Courier New][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()


    [color=darkblue]Dim[/color] Filename [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] wkbOpen [color=darkblue]As[/color] Workbook
    [color=darkblue]Dim[/color] wks [color=darkblue]As[/color] Worksheet


    Filename = Application.GetOpenFilename( _
        "Excel Workbook (*.xlsx), *.xlsx," & _
        "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm," & _
        "Excel 97-2003 Workbook (*.xls), *.xls," & _
        "All Files (*.*), *.*,", 1)
                                            
    [color=darkblue]If[/color] Filename = "False" [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
        
    [color=darkblue]Set[/color] wkbOpen = Workbooks.Open(Filename)
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] wks [color=darkblue]In[/color] wkbOpen.Worksheets
        frmSelectWorksheet.cmbWorkSheet.AddItem wks.Name
    [color=darkblue]Next[/color]
    
    frmSelectWorksheet.Show


[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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