Error when running Macro with other excel workbooks open

VBA_Learner88

New Member
Joined
Apr 17, 2013
Messages
2
I have a Userform that runs a macro to perform a series of operations by reading data from a database I created in the same workbook but on a different sheet. If I have other excel files/workbooks open when I run the macro via the Userform, I get a 'Run Time Error 9: Subscript is out of range'.

What is causing this problem and how can I fix it?

Thanks for your help.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You increase your chances of getting an answer if you post your code.
 
Upvote 0
It is a very big code to post here. Basically, the macro does not work (Cannot use any of the control buttons on the userform) when other excel files are open, instead I get the run time error 9. That is the problem. Here are my theories:

1. The program I wrote is confused as to which workbook to pull data from, thus the out of range error.

Does this make sense?
 
Upvote 0
The program I wrote is confused as to which workbook to pull data from, thus the out of range error

Yes, if the tab you're referring to isn't in the active workbook it would cause a 'Run Time Error 9: Subscript is out of range' error message.

Using the ThisWorkbook function before opening any other file should solve the issue, like so:

Code:
Option Explicit
Sub Macro1()

    Dim wrkMyWorkBook As Workbook
    
    Set wrkMyWorkBook = ThisWorkbook
    
    wrkMyWorkBook.Sheets("Sheet1").Select
    
    Set wrkMyWorkBook = Nothing
    
End Sub

HTH

Robert
 
Upvote 0
It is a very big code to post here. Basically, the macro does not work (Cannot use any of the control buttons on the userform) when other excel files are open, instead I get the run time error 9. That is the problem. Here are my theories:

1. The program I wrote is confused as to which workbook to pull data from, thus the out of range error.

Does this make sense?
Maybe, but that's like asking someone if you have used the right colors for your painting w/o actually showing them the painting. Consider at least posting the portion of the code that generates the error message.
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,460
Members
448,965
Latest member
grijken

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