VBA to only execute an action once

canthony24

Board Regular
Joined
Mar 24, 2016
Messages
70
How do I tell the VBA code to stop a particular action once it has been executed? I have my one of my macros copy an existing worksheet, then it hides it. But once I click the button to execute the macro again, it creates another copy of the worksheet. I only want it to create the copy of worksheet once, then hide it.

Basically, if the new copy is named "Source Data_Post", don't copy the original once the macro is executed again.

Ideas?

Here is the code I have for what I am asking about:

Sheets("Source Data").Select
Sheets("Source Data").Copy After:=Sheets(1)
Sheets("Source Data (2)").Select
Sheets("Source Data (2)").Name = "Source Data_Post"
Sheets("Source Data_Post").Select
ActiveWindow.SelectedSheets.Visible = False
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Code:
i = sheets.count + 1
vName = "Source" & i
activesheet.name = vName
activesheet.Copy before:=Sheets(1)
sheets(1).name = "Source Data"


Sheets(vName).Select
ActiveWindow.SelectedSheets.Visible = False
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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