Macro to insert all borders on all sheets

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
Office Version
  1. 2021
Platform
  1. Windows
I have written code to insert borders on all sheets from row 9 up to the row before where total appears in Col A (could be total repairs, total expenses etc)


I need the borders to be from Col a to the last Column Containing Text in Row 9.


Your assistance is much appreciated



Code:
 Sub Borders_All_Sheets()

Application.ScreenUpdating = False
Dim lngLstCol As Long, lngLstRow As Long

lngLstRow = ActiveSheet.UsedRange.Rows.Count
lngLstCol = ActiveSheet.UsedRange.Columns.Count

For Each rngCell In Range("A9:A" & lngLstRow)
    If rngCell.Value > "" Then
        r = rngCell.Row
        c = rngCell.Column
        Range(Cells(r, c), Cells(r, lngLstCol)).Select
            With Selection.Borders
                .LineStyle = xlContinuous
                .Weight = xlThin
                .ColorIndex = xlAutomatic
            End With
    End If
Next

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
not sure why you would want borders outside your used range,

Code:
dim ws as worksheet

for each ws in activeworkbook.worksheets
    ws.Cells.Select
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
    End With
next ws
 
Last edited:
Upvote 0
I appear to be reading these requests a bit different :confused:

from row 9 up to the row before where total appears in Col A (could be total repairs, total expenses etc
I need the borders to be from Col a to the last Column Containing Text in Row 9

Code:
Sub FindTotal()
    Dim ws As Worksheet, FindTot As Range, myCol As Long, FindRW As Long
    
    For Each ws In ActiveWorkbook.Worksheets
    
        If WorksheetFunction.CountA(ws.Cells) <> 0 Then

            Set FindTot = ws.Columns(1).Find("total*", , xlValues, , xlByRows, xlNext, False)

            If Not FindTot Is Nothing Then
                myCol = ws.Rows(9).Find("*", , xlValues, , xlByColumns, xlPrevious).Column
                FindRW = FindTot.Row - 1
                ws.Range(ws.Cells(9, 1), ws.Cells(FindRW, myCol)).Borders.LineStyle = xlContinuous
            End If
            
        End If
        
    Next
End Sub

Btw, I have interpreted
from row 9 up to the row before where total appears in Col A
to mean
from row 9 down to the row before where total appears in Col A
 
Upvote 0
Thanks for the help. Your interpretation is 100%


Your code works perfectly
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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