Referencing workbook

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
For some reason, the following fails with the error message: Object required

Code:
Sub Test

Dim Mywbs() As Variant 

Mywbs = Array("C:\Test\Apples.xlsm", "C:\Test\Oranges.xlsm")

Dim Counter As Integer

For Counter = 0 to 1

    Call GetWorkbook(Mywbs(Counter)) ' FAILS HERE

Next Integer

End Sub

Sub GetWorkbook(ByVal Mywbs As Workbook)

    Mywbs.Open

End Sub

Please explain what is wrong.

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Mybws is a array of strings, not workbooks.

Try this, it will open each workbook named in the array.
Code:
Sub Test()

Dim Mywbs() As Variant
Dim Counter As Integer

    Mywbs = Array("C:\Test\Apples.xlsm", "C:\Test\Oranges.xlsm")

    For Counter = 0 To 1

        Call GetWorkbook(Mywbs(Counter))    ' FAILS HERE

    Next Counter

End Sub

Sub GetWorkbook(ByVal Mywbs As String)

    Workbooks.Open Mywbs

End Sub
 
Upvote 0
Mybws is a array of strings, not workbooks.

Try this, it will open each workbook named in the array.
Code:
Sub Test()

Dim Mywbs() As Variant
Dim Counter As Integer

    Mywbs = Array("C:\Test\Apples.xlsm", "C:\Test\Oranges.xlsm")

    For Counter = 0 To 1

        Call GetWorkbook(Mywbs(Counter))    ' FAILS HERE

    Next Counter

End Sub

Sub GetWorkbook(ByVal Mywbs As String)

    Workbooks.Open Mywbs

End Sub

Thanks but I prefer Mywbs to be workbook rather than string because later on, I want to refer to the worksheets within the opened workbook.

Something like:

Code:
Dim wks As Worksheet

For Each Mywks in Mywbs.Worksheets

Next Mywks

How can that be done?

I've even tried:

Code:
Dim a As Workbook
Set a = Workbooks (Mywbs)

but it flags up subscript out of range.
 
Last edited:
Upvote 0
You can't have Mywbs as an array of workbooks, the workbooks aren't opened yet.

I suppose you could use something like this.
Code:
Sub Test()

Dim MywbsNames() As Variant
Dim wbs() As Variant
Dim Counter As Integer

    MywbsNames = Array("C:\Test\Apples.xlsm", "C:\Test\Oranges.xlsm")

    For Counter = 0 To 1

        ReDim Preserve wbs(Counter)
        
        Set wbs(Counter) = GetWorkbook(MywbsNames(Counter))

    Next Counter

End Sub

Function GetWorkbook(ByVal Mywbs As String) As Workbook

    Set GetWorkbook = Workbooks.Open(Mywbs)

End Function
 
Upvote 0
This opens the workbook (which is what I want) but I can't do my For Each Loop later on.

Code:
Sub Test

Dim Mywbs() As Variant 

Mywbs = Array("C:\Test\Apples.xlsm", "C:\Test\Oranges.xlsm")

Dim Counter As Integer

For Counter = 0 to 1

    Call GetWorkbook(Mywbs(Counter))

Next Integer

End Sub


Sub GetWorkbook(ByVal Mywbs As String)

Workbook.Open Mywb ' NEW LINE ADDED

End Sub


ie

Code:
Dim wks As Worksheet

For Each Mywks in Mywbs.Worksheets ' FAILS

Next Mywks
 
Upvote 0
That's because Mywbs is still only an array of strings.

Even if it was you an array of workbooks you couldn't use that For Each, you would need a loop for the array too.
 
Upvote 0
Sorted!

Code:
Dim wbname As String

wbname = Right(Mywb, Len(Mywb) - InStrRev(Mywb, "\", Len(Mywb)))

Workbooks.Open

Dim wb As Workbook

Set wb = Workbooks(wbname)

Dim MyWks As Worksheet

For Each MyWks In wb.Worksheets


Next MyWks
 
Upvote 0

Forum statistics

Threads
1,225,481
Messages
6,185,233
Members
453,283
Latest member
Shortm88

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