VBA - Using AND for conditonal formattiing

fluffynicesheep

Board Regular
Joined
Oct 27, 2009
Messages
69
Hi,

I currently have some vba code that looks at column BM and if the cell is empty then it sets the interior colour to white for all cells in the row (from BM to BZ). If there is something in the cell in column BM then it changes everything in the above range to yellow.

What I want to add to this (but have no idea how) is - if the cell in column BM is not empty, but one of the cells (in the same row) from BN to BZ is empty, then it turns that particular cell to red (with rest staying yellow)

Here's what I have at the moment:

VBA Code:
Sub Colour()
   Dim i As Long, r1 As Range, r2 As Range
Dim LastRow As Long
    LastRow = Range("B" & Rows.Count).End(xlUp).Row
  
   For i = 2 To LastRow
      Set r1 = Range("BM" & i)
      Set r2 = Range("BM" & i & ":BZ" & i)
      If r1.Value = "" Then r2.Interior.Color = vbWhite
      If r1.Value <> "" Then r2.Interior.Color = vbYellow
   
   Next i
End Sub

Any help would be greatly received!!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
VBA Code:
Sub Colour()
   Dim i As Long, r1 As Range, r2 As Range
   Dim LastRow As Long
   LastRow = Range("B" & Rows.Count).End(xlUp).Row
  
   For i = 2 To LastRow
      Set r2 = Range("BM" & i & ":BZ" & i)
      If Range("BM" & i) = "" Then
         r2.Interior.Color = vbWhite
      Else
         r2.Interior.Color = vbYellow
         r2.SpecialCells(xlBlanks).Interior.Color = vbRed
      End If
   
   Next i
End Sub
 
Upvote 0
Hi Fluff,

Unfortunately when running this i get a run-time error 1004 - No cells were found.

It then highlights the line:

VBA Code:
r2.SpecialCells(xlBlanks).Interior.Color = vbRed

This may be because there may not always be blank cells in the range (ideally there won't be, as a blank cell means someone hasn't filled in what I need them to fill in!)
 
Upvote 0
Ok, how about
VBA Code:
Sub Colour()
   Dim i As Long, r1 As Range, r2 As Range
   Dim LastRow As Long
   LastRow = Range("B" & Rows.Count).End(xlUp).Row
  
   For i = 2 To LastRow
      Set r2 = Range("BM" & i & ":BZ" & i)
      If Range("BM" & i) = "" Then
         r2.Interior.Color = vbWhite
      Else
         r2.Interior.Color = vbYellow
         On Error Resume Next
         r2.SpecialCells(xlBlanks).Interior.Color = vbRed
         On Error GoTo 0
      End If
   Next i
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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