Simple Activate workbooks code not working

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have some basic code which should activate two different open workbooks.

But I get a run time error ("Subscript out of range") when I execute the code.

Any ideas why? The code is below and I copied and pasted the file names into the code.

Thanks in advance.


Sub ReferringToWorkbooks()


Workbooks("Book2.xlsx").Activate
Workbooks("VBA Lesson 7 - (Wise Owl).xlsm").Activate


End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hello Mr2017, Please check on the following points.
1] Name of the workbooks
2] Both file's should be open before you run this code
Regards,
Thangavel D
 
Upvote 0
Hi,

Change the name of the file from VBA Lesson 7 - (Wise Owl).xlsm to VBA Lesson 7 - (Wise Owl.xlsm or
VBA Lesson 7 - Wise Owl.xlsm


 
Upvote 0
Hi admiral100

Thanks for the prompt response.

Although "Book2" was open, I hadn't saved the file.

So the code would work if I removed the file extension from the activate line, but it wouldn't work if I left the file extension in, as the file wasn't saved.

This is bizarre, as I was able to execute other things from a VBA video series I'm watching with a "Book2" file that was open but not saved.

I made a slight change to the second line, following your advice, and both lines are now working.

Thanks for your help!









Hi,

Change the name of the file from VBA Lesson 7 - (Wise Owl).xlsm to VBA Lesson 7 - (Wise Owl.xlsm or
VBA Lesson 7 - Wise Owl.xlsm


 
Upvote 0
Switching between workbooks using the index number is still not working, however.

I have two open (and saved) workbooks open, but the code below does NOT switch between them.

Any ideas why? Thanks in advance.

Sub ReferringToWorkbooksByIndexNumber()


Workbooks(2).Activate


Workbooks(1).Activate


End Sub
 
Upvote 0
Can you add 2 line to your code and run

Count = Workbooks.CountMsgBox "Number of open Workbooks: " & Count

What is the number ?

By the way your code "switching" between the workbooks only once...
Its activate workbook 2 and then activate workbook 1....so you will always see the first workbook....
 
Upvote 0
Hi

I added that code but it gave me a syntax error?

Can you add 2 line to your code and run

Count = Workbooks.CountMsgBox "Number of open Workbooks: " & Count

What is the number ?

By the way your code "switching" between the workbooks only once...
Its activate workbook 2 and then activate workbook 1....so you will always see the first workbook....
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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