Using Macro from one worksheet in another - runtime error 9

Hello,
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.

 ActiveWorkbook.Worksheets("ORDET20412").Sort.SortFields.Clear

Open in new window

module.docx
eshiramAsked:
Who is Participating?
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:
Hi,
This is because the workbook does not have the sheet named "ORDET20412".
Please check.
0
NorieVBA ExpertCommented:
Where is the code located and which worksheet/workbook should it be executed on?
0
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Saqib Husain, SyedEngineerCommented:
Try

Activesheet.Sort.SortFields.Clear
0

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).
0
Saqib Husain, SyedEngineerCommented:
You use it in place of the line you have shown in the snippet
0
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.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.