Patrick O'Dea
asked on
My Macros is "working" on the wrong work book
Hi,
I have a workbook (WorkBookA) with about 30 sheets in it.
It is created by an external piece of software.
I have an existing workbook which "Stores" macros (WorkBookB).
One of the Macros in WorkbookB sorts the sheets in alpha order. (I want this to happen in WorkbookA NOT WorkBookB)
Problem: When I run the "SortSheets" macro it sorts the sheets in WorkBookB. How do I force it to sort the sheets in WorkBookA.
WorkBookB.xlsm
I have a workbook (WorkBookA) with about 30 sheets in it.
It is created by an external piece of software.
I have an existing workbook which "Stores" macros (WorkBookB).
One of the Macros in WorkbookB sorts the sheets in alpha order. (I want this to happen in WorkbookA NOT WorkBookB)
Problem: When I run the "SortSheets" macro it sorts the sheets in WorkBookB. How do I force it to sort the sheets in WorkBookA.
WorkBookB.xlsm
ASKER
Thanks imnorie,
I follow your logic but cannot quite get what I want.
"How do you run the code?" ---> Both workbooks are open. Click to view macros and then "run".
Do you open the other workbook manually then run it?" --->YES
OKAY, I have tried to simply things further and attach a WorkBookA and WorkBookB.
To clarify; The code is stored in "B" but run from "A" and should update "A". Note that "A" book will have a variable name - because there is a time/date stamp at the end of it.
Bottom Line: I simply want to sort the sheets in "A" into alphabetically order by using the existing macro in "B"
WorkBookB.xlsm
WorkBookA.xlsm
I follow your logic but cannot quite get what I want.
"How do you run the code?" ---> Both workbooks are open. Click to view macros and then "run".
Do you open the other workbook manually then run it?" --->YES
OKAY, I have tried to simply things further and attach a WorkBookA and WorkBookB.
To clarify; The code is stored in "B" but run from "A" and should update "A". Note that "A" book will have a variable name - because there is a time/date stamp at the end of it.
Bottom Line: I simply want to sort the sheets in "A" into alphabetically order by using the existing macro in "B"
WorkBookB.xlsm
WorkBookA.xlsm
Did you try the code I posted?
I know I got the workbook names the wrong way round, but all you need to do is change the workbook name here.
In fact here's an updated WorkbookB.
I've put the SortSheets code in a standard module, which is kind of where this sort of code should go.
You'll still find it in the list of Macros.
WorkBookBV2.xlsm
I know I got the workbook names the wrong way round, but all you need to do is change the workbook name here.
strWBName = "WorkBookB.xlsm" ' change to WorkbookA.xlsm
In fact here's an updated WorkbookB.
I've put the SortSheets code in a standard module, which is kind of where this sort of code should go.
You'll still find it in the list of Macros.
WorkBookBV2.xlsm
ASKER
Thanks again imnorie,
However, my WorkBookB has a variable name (i.e. it has a date/time stamp on the end of it).
Your sort macro crashes with error 1004 on the line
Set wbA = Workbooks.Open(ThisWorkboo k.Path & Application.PathSeparator & strWBName)
I feel that it is probably something very simple that I need.
I.e. How do I open WorkBookB and then ensure that the macro works in WorkBookB.
Can I just "activate" WorkBookB and this will ensure that this is where the action happens??
Apologies for ongoing delays.
However, my WorkBookB has a variable name (i.e. it has a date/time stamp on the end of it).
Your sort macro crashes with error 1004 on the line
Set wbA = Workbooks.Open(ThisWorkboo
I feel that it is probably something very simple that I need.
I.e. How do I open WorkBookB and then ensure that the macro works in WorkBookB.
Can I just "activate" WorkBookB and this will ensure that this is where the action happens??
Apologies for ongoing delays.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks , I've got it now.
I appreciate your patience!
I appreciate your patience!
That will mean that it will run on whatever workbook VBA regards as being active.
How do you run the code?
Do you open the other workbook manually then run it?
Is the workbook tou want to run the code on always named 'WorkbookB'?
If it's just the SortSheets sub you want fixed, you can try this.
Open in new window