How to tell a macro what sheet to run on?

nbraybrook

New Member
Joined
Aug 3, 2011
Messages
7
I made some macros that ran fine, until they wanted all the macros to be placed on a specific worksheet. I have all my macros now on one worksheet, but the macros do not work now I assume because they are not located on the worksheet they ran on anymore? What code do I add to my macros to tell them to run on sheet1 even if they are on sheet2? Or just code that will let me tell it what worksheet to run on? Thanks in advance!

Code:
Sub ENGREV()
'
' ENGREV Macro
'

'
    Rows("5:5").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("C5:U5").Select
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("C6:U6").Select
    Range("U6").Activate
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Range("C5").Select
End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Who are "they"?

The code should go on a regular module, then it will operate on whichever sheet is selected.
 
Upvote 0
Sorry "they" are my bosses. They want all the macros tied to one sheet so they can insert this sheet into older workbooks and "retrofit" older workbooks with all the new macro functions. Problem I am having is macrons run fine on the sheet they are on but i cant get them to run on other sheets in the workbook. Its like the macro doesnt know to go to that sheet. Sorry im an excel noobie.
 
Upvote 0
Well "they" are mental. see if this works

Code:
Sub ENGREV()
'
' ENGREV Macro
'

'
With ActiveSheet
    .Rows("5:5").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    'Range("C5:U5").Select
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    .Range("C6:U6").Select
    .Range("U6").Activate
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    .Range("C5").Select
End If
End Sub
 
Upvote 0
Remove the "End If" (2nd to last line of code)

You show an End if, but no if statement to end.

Hope this helps out.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,786
Members
452,942
Latest member
VijayNewtoExcel

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