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
 

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).
Ok I figured it out. I recorded what I was doing again and going by sheet .Select it works and leaves me on the sheet I need. Even though I shouldn't be using .Select on this below this works.

Code:
Sub MoveCopy()
Sheets("Name Correction").Select
Sheets("Name Correction").Copy Before:=Workbooks( _
        "FIle Zed Upload For Time Macro 2.xlsm").Sheets(1)
        Sheets("Name Correction").Move After:=Sheets(4)
        Sheets("Name Correction").Select
End Sub

And this will leave me on --->FIle Zed Upload For Time Macro 2.xlsm

Reason no clue, but you might see this and be like O.
 
Upvote 0
Hi I'm back maybe one more thing that may help figure out whats wrong. So the last code worked at home last night on Excel 2010. I am trying it now on 2016 at work and again it wont leave me on the sheet I need. Does this ring a bell of what my issue might be?
 
Upvote 0
Hi I'm back maybe one more thing that may help figure out whats wrong. So the last code worked at home last night on Excel 2010. I am trying it now on 2016 at work and again it wont leave me on the sheet I need. Does this ring a bell of what my issue might be?
When you refer to going through your code "in slow motion", do you mean using the F8 function key or the stepping icon on the tool bar to step through the code? Stepping through the code should identify the problem if it is caused by other code. It should also allow you to see where in your code there is lack of response to command in the code.
 
Last edited:
Upvote 0
HI,
I step threw the code using the stepping icon red dots.
If I go threw it in slow motion it works. If I run this small piece at once. Then it doesn't leave me on the sheet I need it to.

Code:
Sub MoveCopy()
    Sheets("Name Correction").Select
    Sheets("Name Correction").Copy Before:=Workbooks( _
    "FIle Zed Upload For Time Macro 2.xlsm").Sheets(1)   '<-- Sheet I need to end on.
    Windows("FIle Zed Upload For Time Macro 2.xlsm").Activate
    Sheets("Name Correction").Select
    Sheets("Name Correction").Move After:=Sheets(4)
End Sub

When I run this in one shot doesn't leave me there. If I do it in slow motion then it does. The option ill have to go to now is close the first sheet and then it will leave me on this. Then open again when needed, but I don't want to give up to find out why its not working this way. I'm curious and I shouldn't have to close first sheet ot leave me on here you know.
 
Upvote 0
So I was looking at another post on here that had same issue. It ended that everyone had no more options to help. So I am not alone. What I did for now is removed this from code above.

"FIle Zed Upload For Time Macro 2.xlsm").Sheets(1)

and made a new Sub to run after this sub to leave me on the sheet I need which works for whatever reason.

Code:
Sub Act()
Windows("FIle Zed Upload For Time Macro 2.xlsm").Activate
Sheets(1).Activate
End Sub
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
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