Cycling through worksheets in a different workbook

ccordner

Active Member
Joined
Apr 28, 2010
Messages
344
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
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
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]
 

Watch MrExcel Video

Forum statistics

Threads
1,122,334
Messages
5,595,567
Members
413,996
Latest member
mabelO

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