VBA if a row is all blank in a range of columns, highlight the blanks and the headers

AnnAnn

New Member
Joined
Mar 26, 2024
Messages
36
Office Version
  1. 2016
Hello,
This code works but takes 11 seconds to run on a 500 row sheet; it's part of a large main macro and I need it to run faster. I've searched Google, YouTube, and other threads on this site but can't find what I need.
Dim lastRow As Long, lastCol As Long, i As Long
Dim col As Variant
Dim headerCell As Range

lastCol = last_col(ws)
lastRow = Last_Row_For_Realsies(ws, lastCol)

Dim checkColumns As Variant
checkColumns = Array("A", "B", "C", "D", "E", "F", "G", "H", "K", "M", "P", "Q", "S", "U", "W", "Y", "AA")

For i = 3 To lastRow
Dim allBlank As Boolean

allBlank = True


For Each col In checkColumns
If ws.Cells(i, col).Value <> "" Then
allBlank = False
Exit For
End If
Next col

If allBlank Then

For Each col In checkColumns
ws.Cells(i, col).Interior.Color = RGB(255, 204, 0)
Set headerCell = ws.Cells(2, col)
headerCell.Interior.Color = RGB(0, 0, 0)
headerCell.Font.Color = RGB(255, 255, 255)
Next col
End If

Next i
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Are you sure it is this portion that is slow? To verify I would put Debug.Print Time before and after each For loop. Next time you run it, check the immediate window for the start and stop times for each loop. That should tell you how long each one takes.

EDIT -Please post code, properly indented, between vba code tags. You click vba button on posting toolbar to get the tags.
 
Upvote 0
Hi to all.
I'm assuming you also have a few formulas throughout your sheet so, maybe, if you add this at the beginning of the macro:
VBA Code:
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
and this at the end:
Code:
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .Calculate
End With
your highlighting will seem faster.
 
Upvote 0
Are you sure it is this portion that is slow? To verify I would put Debug.Print Time before and after each For loop. Next time you run it, check the immediate window for the start and stop times for each loop. That should tell you how long each one takes.

EDIT -Please post code, properly indented, between vba code tags. You click vba button on posting toolbar to get the tags.
Yes, I am sure. I commented out the other subs of the main macro and ran just this one and it took 11 seconds.
 
Upvote 0
Hi to all.
I'm assuming you also have a few formulas throughout your sheet so, maybe, if you add this at the beginning of the macro:
VBA Code:
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With
and this at the end:
Code:
With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .Calculate
End With
your highlighting will seem faster.
Ahhh thank you, and my apologies for not mentioning this in my original post, but yes, I have those at the beginning and end of the main macro.
 
Upvote 0
Are you sure it is this portion that is slow? To verify I would put Debug.Print Time before and after each For loop. Next time you run it, check the immediate window for the start and stop times for each loop. That should tell you how long each one takes.

EDIT -Please post code, properly indented, between vba code tags. You click vba button on posting toolbar to get the tags.
I clicked the vba button but it just added
Code:
 at the beginning, it didn't indent.
 
Upvote 0
See if this macro makes a difference. You will have to modify it to include the "ws" variable.
VBA Code:
Sub ColorCells()
    Dim lRow As Long, i As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 3 To lRow
        If WorksheetFunction.CountA(Intersect(Rows(i), Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA"))) <> 17 Then
            Intersect(Rows(i), Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Interior.Color = RGB(255, 204, 0)
            Intersect(Rows(2), Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Interior.Color = RGB(0, 0, 0)
            Intersect(Rows(2), Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Font.Color = RGB(255, 255, 255)
        End If
    Next i
End Sub
 
Upvote 0
See if this macro makes a difference. You will have to modify it to include the "ws" variable.
VBA Code:
Sub ColorCells()
    Dim lRow As Long, i As Long
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 3 To lRow
        If WorksheetFunction.CountA(Intersect(Rows(i), Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA"))) <> 17 Then
            Intersect(ws.Rows(i), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Interior.Color = RGB(255, 204, 0)
            Intersect(ws.Rows(2), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Interior.Color = RGB(0, 0, 0)
            Intersect(ws.Rows(2), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Font.Color = RGB(255, 255, 255)
        End If
    Next i
End Sub
Thank you, mumps. I received run-time error 1004 at the 'If WorksheetFunction.....' line of code. The only thing I added to your code was ws in front of Cells, Rows and Range.
VBA Code:
Dim lRow As Long, i As Long
    lRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For i = 3 To lRow
        If WorksheetFunction.CountA(Intersect(ws.Rows(i), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA"))) <> 17 Then
            Intersect(ws.Rows(i), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Interior.Color = RGB(255, 204, 0)
            Intersect(ws.Rows(2), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Interior.Color = RGB(0, 0, 0)
            Intersect(ws.Rows(2), ws.Range("A:H,K:K,M:M,P:Q,S:S,U:U,W:W,Y:Y,AA:AA")).Font.Color = RGB(255, 255, 255)
        End If
    Next i
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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