Conditional formating VBA

Peltz

Board Regular
Joined
Aug 30, 2011
Messages
87
Hi there.


I've written this simple code.
PHP:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
For i = 6 To 20
    If ((Cells(i, "7:21").Value = "Yes" Or "No")) Then
        Cells(i, 6) = Selection.FormatConditions(1).Interior.Color = 3
        End If
        
Next
End Sub

What I want it to do is to thange background of the cell in the 6th (J) collumn if all the cells in the 7:21 collumns values is ether "Yes" or "No".
I actually want to use a custom color, but before ill focus on that, ill need the code to work as it is.

Ive reached my limits here, so any help will be much appreciated :)

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:

Code:
If WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "Yes") + WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "No") = Cells(i, 7).Resize(1, 15).Count Then
    Cells(i, 6).Interior.ColorIndex = 3
End If
 
Upvote 0
Try:

Code:
If WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "Yes") + WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "No") = Cells(i, 7).Resize(1, 15).Count Then
    Cells(i, 6).Interior.ColorIndex = 3
End If


First of all. I find the learning curve for programming quite steep. At the same time, I've never met so many people eager to help on this forum, which make the whole ordeal so much fun! So thank you so much for your time!

By this:

Code:
Try:

Code:

If WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "Yes") + WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "No") = Cells(i, 7).Resize(1, 15).Count Then    Cells(i, 6).Interior.ColorIndex = 3End
</PRE>

I figure you ment this?:
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
For i = 6 To 20
If WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "Yes") + WorksheetFunction.CountIf(Cells(i, 7).Resize(1, 15), "No") = Cells(i, 7).Resize(1, 15).Count Then
    Cells(i, 6).Interior.ColorIndex = 3
End If
Next
End Sub

I dont see the cell changing background color after all the cells include a "yes" or a "no". This is the first time ive seen the rezise function. Google tells me it returns a value representing a range. BUT, the countIF function made me find a way to use it in the workshet, so thanks a bunch!... By the way, could you tell me what you tried doing by using the rezise function, and maybe a reason why the code didn't work.
 
Upvote 0

Forum statistics

Threads
1,223,430
Messages
6,172,065
Members
452,444
Latest member
ShaImran193

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