Link to home
Start Free TrialLog in
Avatar of Patrick O'Dea
Patrick O'DeaFlag for Ireland

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
Avatar of Norie
Norie

There are no workbook (or worksheet) references in the code.

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.
Sub SortSheets()
Dim wbB As Workbook
Dim strWBName As String
Dim lCount As Long, lCounted As Long
Dim lShtLast As Long


    strWBName = "WorkBookB.xlsm"    ' change name if required

    ' set reference to workbook to run code on
    On Error Resume Next
    Set wbB = Workbooks.Open(strWBName)

    On Error GoTo 0

    If wbB Is Nothing Then

        Set wbB = Workbooks.Open(ThisWorkbook.Path & Application.PathSeparator & strWBName)

    End If

    lShtLast = wbB.Sheets.Count

    For lCount = 1 To lShtLast

        For lCount2 = lCount To lShtLast

            If UCase(wbB.Sheets(lCount2).Name) < UCase(wbB.Sheets(lCount).Name) Then

                wbB.Sheets(lCount2).Move Before:=wbB.Sheets(lCount)

            End If

        Next lCount2

    Next lCount
End Sub

Open in new window

Avatar of Patrick O'Dea

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
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.
 strWBName = "WorkBookB.xlsm" ' change to WorkbookA.xlsm

Open in new window


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
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(ThisWorkbook.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.


ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks , I've got it now.

I appreciate your patience!