Convert String to Workbook object

dhancy

Board Regular
Joined
Jul 10, 2013
Messages
123
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have this code:

Code:
Dim myWB as string 
Dim ws as worksheet

myWB = "myWorkBook.xlsx"

I now want to refer to myWB as a Workbook object.

In other words, I'd like do something like this:

Code:
for each ws in my WB
...
next s


Any ideas how to accomplish this? Thanks!


Dennis
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Like
Code:
Dim myWB As Workbook
Dim ws As Worksheet

Set myWB = Workbooks("myWorkBook.xlsx")
 
Upvote 0
Like
Code:
Dim myWB As Workbook
Dim ws As Worksheet

Set myWB = Workbooks("myWorkBook.xlsx")

Thanks. Funny thing is, I tried that, but it gave me an error: "Run-time error '9': Subscript out of range".

Is there something else I am missing?
 
Upvote 0
Either the workbook isn't open, or the name (including extension) is wrong.
 
Upvote 0
Hello dhancy,

The error indicates that the workbook is not open in your current Excel application.

Code:
Dim MyWB As Worksbook
Dim ws As Worksheet

    On Error Resume Next
        Set MyWB = Workbooks("myWorkBook.xlsx")
        If Err = 9 Then Set MyWB = Workbooks.Open("myWorkBook.xlsx")   ' You may need to include the full path to open the workbook.
    On Error GoTo 0
 
Upvote 0
To expand upon what Leith said, the workbook not only has to be open, but it has to be open in the same Excel session.
Depending on how you are opening the file, you can have different Excel sessions open on your computer, and they won't be able to see one another.
If you open the files separately from Windows Explorer, this will typically be the case.
 
Upvote 0
To expand upon what Leith said, the workbook not only has to be open, but it has to be open in the same Excel session.
Depending on how you are opening the file, you can have different Excel sessions open on your computer, and they won't be able to see one another.
If you open the files separately from Windows Explorer, this will typically be the case.

Thanks Joe4, Leith, and Fluff!

It's working now. I appreciate your help on this :-)

dennis
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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