Trying to get this small vba code to work the right way, but cant figure it out

zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,125
Office Version
  1. 365
Platform
  1. Windows
As you may see its probably not the best way efficient, but it works almost the way I want it to except. At the end of macro I need it to leave me on workbook

Workbooks("FIle Zed Upload For Time Macro 2.xlsm").Activate

Which for some reason it doesn't now. If I play it slow motion then it does. So I'm confused on where the issue is.
Code:
Sub MoveCopy()
    Sheets("Name Correction").Copy Before:=Workbooks( _
    "FIle Zed Upload For Time Macro 2.xlsm").Sheets(1)
    Windows("FIle Zed Upload For Time Macro 2.xlsm").Activate
    Sheets("Name Correction").Move After:=Sheets(4)
    Workbooks("FIle Zed Upload For Time Macro 2.xlsm").Activate
End Sub
 
Hi so I'm back at it all day right. So it looks like the "Time" book is not the active sheet at that moment of code. Its the one I want to be active, but for some reason it wont view it on my screen. So I made the "Time" Active and then made the one I want active and it works. Look at code now for whatever reason this is I have no idea.


Code:
Sub MoveCopy()
    Sheets("Name Correction").Copy Before:=Workbooks( _
    "File Zed Upload For Time Macro 2.xlsm").Sheets(1)
    Sheets("Name Correction").Move After:=Sheets(4)
    Sheets(1).Activate
    Windows("Time").Activate     ' I have to activate
    Windows("FIle Zed Upload For Time Macro 2.xlsm").Activate  'Then activate this and then it shows this book in view as active' Not a clue why
End Sub
It sounds to me like your code resides in the "Time" workbook and not in the "File Zed..." workbook as originally stated. In any event, to activate a sheet when more than one workbook is open, you must first activate the workbook, then the sheet.
Code:
Workbooks("Time").Activate
Sheets("Sheet1").Activate
 
Last edited:
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
​Hi thanks for the reply. So this option doesn't work either.
Code:
Workbooks("Time").Activate
Sheets("Sheet1").Activate

Plus vba code is only in workbook FIle Zed Upload For Time Macro 2.xlsm. There is nothing in the Time one

Also I am thinking today since maybe the tab name "Name Correction" is getting moved and copy to this other sheet is confusing the code to believe its not actually active.
 
Upvote 0
​Hi thanks for the reply. So this option doesn't work either.
Code:
Workbooks("Time").Activate
Sheets("Sheet1").Activate

Plus vba code is only in workbook FIle Zed Upload For Time Macro 2.xlsm. There is nothing in the Time one

Also I am thinking today since maybe the tab name "Name Correction" is getting moved and copy to this other sheet is confusing the code to believe its not actually active.
That was only an example to illustrate the sequence needed to activate a sheet in a workbook. The workbook has to be made active first, then the sheet. That is why I stay away from the Activate and Select syntaxes as much as possible. Direct coding is much better than the syntax used by the macro recorder.
Regards, JLG
 
Upvote 0
That was only an example to illustrate the sequence needed to activate a sheet in a workbook. The workbook has to be made active first, then the sheet. That is why I stay away from the Activate and Select syntaxes as much as possible. Direct coding is much better than the syntax used by the macro recorder.
Regards, JLG

I know i agree. The only way for now that i got it to work is doing this. After it opens workbook it thinks even if you activate the other book that the active sheet is the wrong sheet. i don't know
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top