Formatting Multiple Sheets in Terms of Cell and Font Color using VBA

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I currently have this code, which applies to a border to all cells with information in all worksheets in a given workbook. I would now like to add to this code so that it:
- Changes all cells to one color
- Changes the left most two columns to another color
- Changes the top two rows to the same color and change the font to the same color
(I have provided what my code currently outputs and what I want the goal output to be)

I have multiple worksheets I need to do this for but the last column/ last row with information varies from worksheet to worksheet

VBA Code:
Sub AllWorksheetBorders()

    Application.ScreenUpdating = False    'Prevents screen refreshing
    Dim lngLstCol As Long, lngLstRow As Long, ws As Worksheet
    Dim rngCell As Range, r As Long, c As Long

    For Each ws In ActiveWorkbook.Worksheets
        lngLstRow = ws.UsedRange.Rows.Count
        lngLstCol = ws.UsedRange.Columns.Count

        For Each rngCell In ws.Range("A1:A" & lngLstRow)
            If rngCell.Value <> "" Then
                r = rngCell.Row
                c = rngCell.Column

                With ws.Range(ws.Cells(r, c), ws.Cells(r, lngLstCol)).Borders
                    .LineStyle = xlContinuous    'Setting style of border line
                    .Weight = xlThin    'Setting weight of border line
                    .ColorIndex = xlAutomatic    'Setting colour of border line
                End With
            End If
        Next

Current Output:
Book1345.xlsm
ABCDEFG
1FirstLast12345
2aabbccddee
3AliBrownX
4BillPatelXX
5CatherineGomezXX
6DelilahHarringtonX
7Eva LeeXXXX
8FreyaMillerXXX
9GinaWilliamsXXX
10HerrodSmithXXX
11IsabelleCooperX
12JohnWakefieldXXX
Sheet6


Goal Output:
Book1345.xlsm
ABCDEFG
1FirstLast12345
2aabbccddee
3AliBrownX
4BillPatelXX
5CatherineGomezXX
6DelilahHarringtonX
7Eva LeeXXXX
8FreyaMillerXXX
9GinaWilliamsXXX
10HerrodSmithXXX
11IsabelleCooperX
12JohnWakefieldXXX
Sheet5
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about
VBA Code:
Sub AllWorksheetBorders()

    Application.ScreenUpdating = False    'Prevents screen refreshing
    Dim lngLstCol As Long, lngLstRow As Long, ws As Worksheet
    Dim rngCell As Range, r As Long, c As Long

    For Each ws In ActiveWorkbook.Worksheets
        lngLstRow = ws.UsedRange.Rows.Count
        lngLstCol = ws.UsedRange.Columns.Count

        For Each rngCell In ws.Range("A1:A" & lngLstRow)
            If rngCell.Value <> "" Then
                r = rngCell.Row
                c = rngCell.Column

                With ws.Range(ws.Cells(r, c), ws.Cells(r, lngLstCol)).Borders
                    .LineStyle = xlContinuous    'Setting style of border line
                    .Weight = xlThin    'Setting weight of border line
                    .ColorIndex = xlAutomatic    'Setting colour of border line
                End With
            End If
            With ws.UsedRange
               .Interior.Color = 14083324
               .Columns(1).Interior.Color = 8696052
               .Columns(2).Interior.Color = xlNone
               .Rows("1:2").Interior.Color = 1137094
               .Rows("1:2").Font.Color = vbWhite
            End With
         Next ws
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub AllWorksheetBorders()

    Application.ScreenUpdating = False    'Prevents screen refreshing
    Dim lngLstCol As Long, lngLstRow As Long, ws As Worksheet
    Dim rngCell As Range, r As Long, c As Long

    For Each ws In ActiveWorkbook.Worksheets
        lngLstRow = ws.UsedRange.Rows.Count
        lngLstCol = ws.UsedRange.Columns.Count

        For Each rngCell In ws.Range("A1:A" & lngLstRow)
            If rngCell.Value <> "" Then
                r = rngCell.Row
                c = rngCell.Column

                With ws.Range(ws.Cells(r, c), ws.Cells(r, lngLstCol)).Borders
                    .LineStyle = xlContinuous    'Setting style of border line
                    .Weight = xlThin    'Setting weight of border line
                    .ColorIndex = xlAutomatic    'Setting colour of border line
                End With
            End If
            With ws.UsedRange
               .Interior.Color = 14083324
               .Columns(1).Interior.Color = 8696052
               .Columns(2).Interior.Color = xlNone
               .Rows("1:2").Interior.Color = 1137094
               .Rows("1:2").Font.Color = vbWhite
            End With
         Next ws
End Sub
Thanks I'll try that!
 
Upvote 0

Forum statistics

Threads
1,215,254
Messages
6,123,893
Members
449,131
Latest member
leobueno

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