VBA/Macro "Return to most recent worksheet"

abringley1083

New Member
Joined
Jul 14, 2020
Messages
3
Office Version
  1. 2016
Platform
  1. MacOS
I have a macro I want to iron out... I need to copy/paste specific columns from one work sheet to another. I believe I have it to where when I run the macro, it will successfully copy/paste the columns.

My problem is the way I wrote the macro, it only knows to do it between two certain worksheets. For example, if I want to copy the columns from worksheet X into worksheet Y, it's written to do that. But if I start the macro on worksheet Z (to copy the columns into that particular worksheet), it still copies into worksheet Y.

Basically I want the code "Sheets("Y").Select" to be something like "Sheets(most recent/last visited/something).Select"

Let me know if you can help with this on Mac OS Excel 16.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
But if I start the macro on worksheet Z (to copy the columns into that particular worksheet) , it still copies into worksheet Y.
If that is the case then use ActiveSheet rather than referring to the worksheet name.
 
Upvote 0
If that is the case then use ActiveSheet rather than referring to the worksheet name.
So I tried changing "Y" to ActiveSheet and "ActiveSheet" but I think what happens is when it jumps to Worksheet X to copy the columns, the active sheet changes to worksheet X.

Is that a possible problem?
 
Upvote 0
But if I start the macro on worksheet Z

I thought by what you posted that Z (the sheet being pasted to) was the activesheet.

Post your code (in code tags please) in the thread and describe what you would do if you were doing the task manually.
 
Upvote 0
Apologies, I don't run a mac, but maybe something like this?

ThisWorkbook Code:

Code:
Private Sub Workbook_SheetDeactivate(ByVal Sht As Object)
Set LstSht = Sht
End Sub

Then in a module, reference it using:

Code:
Public LstSht As Worksheet
Sub YourCode()
    LstSht.Activate
End Sub

That wont work if there was never a previous sheet selected, but you could work around that with

Code:
sheets("Sheet1").activate
LstSht.Activate
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,453
Members
452,514
Latest member
cjkelly15

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