How to set another workbook as a variable

chrissnead

New Member
Joined
Apr 10, 2018
Messages
16
I am trying to set an open workbook as variable book2 but keep getting a 'Subscript out of range' error. Below is the code I am working with. It errors at 'Set book2'. It's completely random and will work sometimes but most of the time it doesn't. Could someone help?

Set book1 = ActiveWorkbook
Set book2 = Workbooks("MySpreadsheet.xlsx") ' errors out here


Set lookFor = book1.Sheets(1).Cells(NextRow, 2) ' value to find
Set srchRange = book2.Sheets(1).Range("A:N") 'source


lookFor.Offset(0, 6).Value = Application.VLookup(lookFor, srchRange, 11, False)
lookFor.Offset(0, 7).Value = Application.VLookup(lookFor, srchRange, 10, False)
 
If I use the below it gives me run time error 9 Subscript out of range.

Set book1 = ActiveWorkbook
Set book2 = Workbooks("MySpreadsheet.xlsx")

If I take the route you suggested of doing the below it gives the same error, but this time at the .Activate.

Set book1 = ActiveWorkbook
Workbooks("MySpreadsheet.xlsx").Activate
Set book2 = ActiveWorkbook

Here's what I've got so far:




Sub getInfo()


Dim lookFor As Range
Dim srchRange As Range
Dim book1 As Workbook
Dim book2 As Workbook


Set book1 = ActiveWorkbook
Set book2 = Workbooks("MySpreadsheet.xlsx")


Set lookFor = book1.Sheets(1).Cells(NextRow, 2) ' value to find
Set srchRange = book2.Sheets(1).Range("A:N") 'source


lookFor.Offset(0, 6).Value = Application.VLookup(lookFor, srchRange, 11, False)
lookFor.Offset(0, 7).Value = Application.VLookup(lookFor, srchRange, 10, False)
lookFor.Offset(0, 8).Value = "VISALIA"
lookFor.Offset(0, 9).Value = Application.VLookup(lookFor, srchRange, 3, False)
If lookFor.Offset(0, 9).Value = "RETURNED" Then
lookFor.EntireRow.Copy
book1.Sheets(3).Range("B2").Select
Do While ActiveCell.Value <> ""
ActiveCell.Offset(1, 0).Activate
Loop
ActiveCell.PasteSpecial
lookFor.Select
End If
lookFor.Offset(0, 10).Value = Application.VLookup(lookFor, srchRange, 13, False)
lookFor.Offset(0, 11).Value = Application.VLookup(lookFor, srchRange, 14, False)
lookFor.Offset(0, 12).Value = lookFor.Offset(0, 10).Value + 1

End Sub
 
Last edited:
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What if you change this line:
Code:
[COLOR=#333333]Workbooks("MySpreadsheet.xlsx").Activate[/COLOR]
to this:
Code:
[COLOR=#ff0000]Windows[/COLOR][COLOR=#333333]("MySpreadsheet.xlsx").Activate[/COLOR]

Does that work for you?
 
Upvote 0
It sounds to me like those two files may not really be in the same Excel session.
If you go into the VB Editor, and view the VB Explorer in the left-column, do you see BOTH workbooks listed there?
If not, then they are NOT both in the same Excel session.
 
Upvote 0
I was actually able to get it working finally by using a couple of your tips but declaring a third workbook variable.

Set book1 = ActiveWorkbook
Set book2 = Windows("JB Hunt- FX Express.xlsx")

book2.Activate

Set book3 = ActiveWorkbook

book1.Activate

Thanks Joe4 for the help!
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,598
Members
449,089
Latest member
Motoracer88

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