Ensuring Macros only Affect the Active Sheet

Tanner_2004

Well-known Member
Joined
Jun 1, 2010
Messages
616
Hi Folks:

I store the majority of my macros in my personal macro workbook/file. Is the a way to ensure that when I run a macro that it only affects the ActiveSheet?

My second question is how to identify the WS that I want the macro to run in when storing macros in my personal macro workbook for times when i may not have the active sheet open?

Thank you for reading and any advice.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
You are welcome!

What you may also see sometimes is that a person will put code at the top that activates a specific sheet, i.e.
Code:
Sheets("Sheet1").Activate
That makes that specific sheet the active sheet.

Then you can have code underneath it does not use a sheet reference of any kind (so it defaults to the active sheet).

So instead of using specific sheet references, they will just first make whatever sheet the want to work on the active sheet by selecting/activating it.
That works fine if everything is being done to one specific sheet, but if bouncing back-and-forth between sheets a bunch in your code (like copying/pasting), that can be confusing and your code might not work right if you don't have it written correctly.
 
Upvote 0
Where do I place the Activate method please? Thank you.

Code:
Dim ws As Worksheet
Set ws = Sheets("PositionStatus")
 
Upvote 0
Just under that, you could do:
Code:
ws.Activate

However, in your are only going to be working on this sheet (no other sheets are involved), if would be easier to replace all three of those lines with just the single line:
Code:
Sheets("PositionStatus").Activate
 
Upvote 0
You would need to place it before any code that does not have a sheet reference on the range objects (range(),CELLS()).
So if you are formatting cells on sheet1 activate it and to work with a different sheet you would activate it.
Code:
Sheets("Sheet1").Activate
Range("A2:A7") = Format(Date, "mm/dd/yy")

Sheets("Sheet2").Activate ' to use range objects with out sheet references for cells on sheet2
Range("A2:B7")="HI"
 
Upvote 0
Tony

FWIW you don't need to activate sheets to work with them, and as someone mentioned earlier, activating sheets can cause problems.

Also, you don't need to repeat the worksheet reference everywhere, you can use a With statement.

For example, let's say you had code like this,
Code:
Range("A10").Value = "Total"

Range("B10").Value = "=SUM(B2:B9)"
and you want to apply it to a specific sheet, say 'Sheet3'.

Then you can use something like this.
Code:
With Sheets("Sheet3")
    .Range("A10").Value = "Total"

    .Range("B10").Value = "=SUM(B2:B9)"
End With
Anything in between the With...End With that's preceded by a . refers back to the sheet in the With statement, in this case Sheets("Sheet3").
 
Upvote 0

Forum statistics

Threads
1,215,113
Messages
6,123,165
Members
449,099
Latest member
bes000

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