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
Patrick O'DeaAsked:
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.

NorieVBA ExpertCommented:
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

0
Patrick O'DeaAuthor Commented:
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
0
NorieVBA ExpertCommented:
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
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.

Patrick O'DeaAuthor Commented:
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.


0
NorieVBA ExpertCommented:
You could try activating, but it might not guarantee the action will happen in that workbook.

It's better to have a workbook reference.

Do you want code that will let you select (and open) the workbook that you want the worksheets to be sorted in?

Option Explicit

Sub SortSheets()
Dim wbA As Workbook
Dim strWBName As String
Dim lCount As Long, lCounted As Long
Dim lCount2 As Long

Dim lShtLast As Long


    strWBName = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm", , , , False)
    
    If TypeName(strWBName) = "Boolean" Then
        MsgBox "No workbook selected", vbCritical
    Else

        Set wbA = Workbooks.Open(strWBName)

    End If

    lShtLast = wbA.Sheets.Count

    For lCount = 1 To lShtLast

        For lCount2 = lCount To lShtLast

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

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

            End If

        Next lCount2

    Next lCount
End Sub

Open in new window

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
Patrick O'DeaAuthor Commented:
Thanks , I've got it now.

I appreciate your patience!
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
Microsoft Excel

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.