Run a macro on a specific worksheet

dclrvtt01

New Member
Joined
Sep 2, 2011
Messages
8
I used the recorder to create a macro on 'Sheet2' and it works flawlessly. Then I created a command button on 'Sheet 1' to run the macro on 'Sheet 2'.

Unfortunately, when I click the command button, it only runs on 'Sheet 1'. I tried using the select sheets("Sheet2").activate code, but it wasn't working. I've listed the code below. Can you please help? Thanks in advance.

Sub MMkt_Analysis_Tab()
'
' MMkt_Analysis_Tab Macro
'
'
Rows("24:42").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A6:M23").Select
Selection.Copy
Range("A25").Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=False
Selection.Font.Underline = xlUnderlineStyleDouble
Selection.Font.Underline = xlUnderlineStyleNone
Rows("25:27").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = True
Range("A27:M27").Select
Selection.Font.Underline = xlUnderlineStyleSingle
Selection.Font.Underline = xlUnderlineStyleNone
Range("A27,C27,E27,G27,I27,K27,M27").Select
Range("M27").Activate
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("C40,E40,G40,I40,K40,M40").Select
Range("M40").Activate
Selection.Font.Underline = xlUnderlineStyleSingleAccounting
Range("C42,E42,G42,I42,K42,M42").Select
Range("M42").Activate
Selection.Font.Underline = xlUnderlineStyleDoubleAccounting
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try like this

Code:
Sub MMkt_Analysis_Tab()
'
' MMkt_Analysis_Tab Macro
'
'
With Sheets("Sheet2")
    .Rows("24:42").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    .Range("A6:M23").Copy
    .Range("A25").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    .Range("A25").Font.Underline = xlUnderlineStyleDouble
    .Range("A25").Font.Underline = xlUnderlineStyleNone
    With .Rows("25:27")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    .Rows("25:27").Font.Bold = True
    With .Range("A27:M27")
        .Font.Underline = xlUnderlineStyleSingle
        .Font.Underline = xlUnderlineStyleNone
    End With
    With .Range("M27")
        .Borders(xlDiagonalDown).LineStyle = xlNone
        .Borders(xlDiagonalUp).LineStyle = xlNone
        .Borders(xlEdgeLeft).LineStyle = xlNone
        .Borders(xlEdgeTop).LineStyle = xlNone
        With .Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        .Borders(xlEdgeRight).LineStyle = xlNone
        .Borders(xlInsideVertical).LineStyle = xlNone
        .Borders(xlInsideHorizontal).LineStyle = xlNone
    End With
    With .Range("C40,E40,G40,I40,K40,M40")
        .Font.Underline = xlUnderlineStyleSingleAccounting
    End With
    With .Range("C42,E42,G42,I42,K42,M42")
        .Font.Underline = xlUnderlineStyleDoubleAccounting
    End With
End With
End Sub
 
Upvote 0
Thanks for the quick reply. Unfortunately, when I copied the code, I got a Compile error. Syntax error. Any thoughts?
 
Upvote 0
Excuse my ignorance since I'm new to VBA, but if it is the line highlighted in yellow, then it is the first line Sub MMkt_Analysis_Tab(). What do you think?
 
Upvote 0
Thanks for your help. Sorry it took me so long to get back to you. It is working except for one issue. When I run the macro through a control button, it adds a double underline in each copied cell. However, if I hit the play button in VBA, it doesn't add the double underline. Do you know what would be causing that? Thanks again.
 
Upvote 0
To clarify things, it only adds the double underline if the control button is on a different worksheet within the workbook. I added one to the selected worksheet and it works fine.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

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