Change VBA to work on Active Sheet

CB_DFW

Registered User.
Local time
Yesterday, 20:55
Joined
Nov 5, 2008
Messages
30
I am using this code to format some borders on my worksheet. The worksheet name is "District". How can I change the code to work on the active sheet?

One reason that I need to do this is because users are copying the worksheet to other files and it copies the format button with it so I need it to work on any sheet that they paste it too.

Also, another thing that might work is if you could prevent the button from being copied when they chose to copy and paste the entire worksheet. Is there a way to do this?

File is attached.

Thanks

Code:
Public Sub FormatBorders()
Dim lRow As Long
Dim rngFind As Range
Dim firstFind As Variant
lRow = Application.ActiveSheet.UsedRange.Rows.Count
With Worksheets("District").Range("A4:a" & lRow)
    Set rngFind = .Find("", LookIn:=xlValues)
    If Not rngFind Is Nothing Then
        firstFind = rngFind.Address
        Do
        With Range("A" & rngFind.Row & ":x" & rngFind.Row)
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeTop).ColorIndex = xlAutomatic
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlMedium
.Borders(xlEdgeBottom).ColorIndex = xlAutomatic
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
End With
 
            Set rngFind = .FindNext(rngFind)
        Loop While Not rngFind Is Nothing And rngFind.Address <> firstFind
    End If
End With
Range("A4").Select
 
End Sub
 

Attachments

Last edited:
All you need to do is change this line:
Code:
With Worksheets("District").Range("A4:a" & lRow)
to this:
Code:
With Application.ActiveSheet.Range("A4:a" & lRow)
 

Users who are viewing this thread

Back
Top Bottom