RT Error '9': Subscript out of range when using Workbooks.activate and Sheets.activate

faemike

New Member
Joined
Dec 8, 2017
Messages
6
Hello,

I'm running into some behavior that I can't explain. I've created an xlsm file which I use to sort a separate workbook (workbook A) with, at first, only a single sheet (sheet A).

I've written the code so that it takes a text box input from a form in the xlsm file (a person's last name) and on a button_click event, it captures the value and stores it as a variable. It then calls Sub M with the captured value as a parameter. This sub then activates workbook A and then immediately activates sheet A. The first time that this sub is called, I have no issues. However, on every subsequent call of this sub, I get an RT Error '9'. Interestingly, if I click "debug" and then "continue"(F5), the script completes as expected so it's not a syntax-related issue though I wonder if I'm using these calls properly...

Code:
Workbooks("Workbook.xls").Activate
Sheets("Sheet").Activate

I was originally activating the workbook in the button_click sub of the macro file and then reactivating it in Sub M but I've ruled that out as an issue by simply commenting out this workbook.activate.


Any ideas or recommendations on what to look at?
 
With all due respect, as you are a self admitted rookie, a rookie mistake would be not using Option Explicit on every code you write.
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

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