Using Macro from one worksheet in another - runtime error 9

I have a user who works on a lot of Excel files with the same format (the actual content is different, but the columns and format is the same). He created a macro in one of them, and when running it in another spreadsheet it gives a runtime error. It says, run time error 9 subscript out of range. When I click debug it shows the file name of the excel file where the macro was created and not the current excel file. How can I change that to work in any excel file? Below is one line in question. I will attach the full macro module.


Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramesh VCommented:
This is because the workbook does not have the sheet named "ORDET20412".
Please check.
NorieAnalyst Assistant Commented:
Where is the code located and which worksheet/workbook should it be executed on?
eshiramAuthor Commented:
No it does not have a sheet with that name, I know that. The macro created in that sheet is being run on a completely different workbook.

It was saved in the Personal Macro Workbook. It was created in one workbook and I want to use it in another workbook. However, it is not working in that second workbook because of the file name, which I cannot figure out how to change for future workbooks.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Saqib Husain, SyedEngineerCommented:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eshiramAuthor Commented:
@Saqib, where exactly do I put that? Or do I replace it with something else? I do not know anything about about VB (clearly).
Saqib Husain, SyedEngineerCommented:
You use it in place of the line you have shown in the snippet
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.