VBA - Need help formatting a row range based on string or format in column A

Matt__S

New Member
Joined
Mar 26, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have the code below that works. It searches column A looking for "00" as the left most characters and then formats that cell with white text and black interior.

VBA Code:
Dim cel As Range
For Each cel In Range("A6:A300")
If Left(cel.Value, 2) = "00" Then
cel.Font.Color = vbWhite
cel.Interior.Color = vbBlack
End If
Next

What I need to happen is for it to not only format the cell in Column A but then to copy that same format from column A to column W. Ideally I need it to search column for "00" as the left most characters in column A and then it applies the formatting above from column A to column W. This is an often updated report and where "00" appears varies each time the data is pulled.

I've gotten this code to "work" using entirerow but I do not want the "entirerow" formatted in this way.

Appreciate the help! Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the board. Try:
Code:
Dim x   As Long
Dim LR  As Long

LR = Cells(Rows.Count, 1).End(xlUp).Row

Application.ScreenUpdating = False

For x = 6 To LR
    With Cells(x, 1).Resize(, 23)
        If Left$(.Cells(1, 1).Value, 2) = "00" Then
            .Font.Color = vbWhite
            .Interior.Color = vbBlack
        End If
    End With
Next x

Application.ScreenUpdating = True
 
Upvote 0
Welcome to the board. Try:
Code:
Dim x   As Long
Dim LR  As Long

LR = Cells(Rows.Count, 1).End(xlUp).Row

Application.ScreenUpdating = False

For x = 6 To LR
    With Cells(x, 1).Resize(, 23)
        If Left$(.Cells(1, 1).Value, 2) = "00" Then
            .Font.Color = vbWhite
            .Interior.Color = vbBlack
        End If
    End With
Next x

Application.ScreenUpdating = True

Thank you so much! Worked perfectly!
 
Upvote 0
Welcome to the MrExcel board!

Another option that you may consider is to do them all at once rather than row-by-row along these lines.

VBA Code:
Sub Format_00_Cells()
  With Range("A5:W" & Range("A" & Rows.Count).End(xlUp).Row)
    .AutoFilter Field:=1, Criteria1:="00*"
    If .Columns(1).SpecialCells(xlVisible).Count > 1 Then
      With .Offset(1).Resize(.Rows.Count - 1)
        .Font.Color = vbWhite
        .Interior.Color = vbBlack
      End With
    End If
    .AutoFilter Field:=1
  End With
End Sub
 
Upvote 0
You're welcome and I'd recommend Peter_SSs as a faster solution especially when your data size is large
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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