Improve short macro

Ironman

Well-known Member
Joined
Jan 31, 2004
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
Hi

I used the macro recorder to create part of the macro below to outline the selected cell in red but it seems a bit clunky.
VBA Code:
Sub Outline()

If ActiveSheet.Name <> "Training 1981-1997" Then
      MsgBox "You clicked this by mistake!" & vbNewLine & vbNewLine & _
             "This function will only run in" & vbNewLine & _
             "Training 1981-1997 sheet", vbInformation, "Function Invalid In This Sheet"
      Exit Sub
   End If

    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Color = -16776961
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Color = -16776961
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Color = -16776961
        .Weight = xlMedium
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Color = -16776961
        .Weight = xlMedium
    End With

End Sub
Can this be shortened?

Also, I'd be grateful if a couple of lines could be added so it will not outline more than 1 cell at any one time.

Many thanks!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
How about
VBA Code:
Sub Ironman()

If ActiveSheet.Name <> "Training 1981-1997" Then
      MsgBox "You clicked this by mistake!" & vbNewLine & vbNewLine & _
             "This function will only run in" & vbNewLine & _
             "Training 1981-1997 sheet", vbInformation, "Function Invalid In This Sheet"
      Exit Sub
   End If
   Selection(1).BorderAround , xlMedium, , -16776961

End Sub
 
Upvote 0
Solution
Wow, that's way shorter!

Thanks ever so much Fluff!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,645
Members
448,974
Latest member
DumbFinanceBro

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