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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

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