Why does 2nd cond. format surpass range

BustedAvi

New Member
Joined
Jun 24, 2010
Messages
27
Hello,

I have the following code:

Code:
On Error Resume Next
    xpH = Application.WorksheetFunction.Match("pH", Range("A1:A40"), 0)
    
    Dim rngToFormat11 As Range
   Set rngToFormat11 = ActiveSheet.Rows(xpH).EntireRow
'first, clear any old Cond format for these cells
   rngToFormat11.FormatConditions.Delete

'add cond 1
   rngToFormat11.FormatConditions.Add Type:=xlCellValue, _
       Operator:=xlGreater, Formula1:="=8.5"
   rngToFormat11.FormatConditions(1).Interior.Color = 65535

'add cond 2
   
   rngToFormat11.FormatConditions.Add Type:=xlCellValue, _
       Operator:=xlLess, Formula1:="=6.5”
   rngToFormat11.FormatConditions(2).Interior.Color = 65535

It scans the range (A1:A40) for keyword "pH", then applies cond. formatting to the entirerow.

When I only use 1 condition, the range selected ends at the last column of entered data. However, when I use both conditions, the formatting is applied to the entire row.

I've tried swapping .EntireRow for .End(xlright) but in this case the formatting is not applied whatsoever.

Any help would be greatly appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
If you are more specific about what values you are trying to apply the condition to, I could help you more.

Looks like the code you have doesn't reference a value so it is being applied to any row that contains "Ph" in the "A" column.
 
Upvote 0
Maybe this will help (untested):
Code:
Dim lCol As Long, xpH As Long
xpH = Application.WorksheetFunction.Match("pH", Range("A1:A40"), 0)
lCol = Cells(xpH, Columns.Count).End(xlToLeft).Column
Set rngToFormat11 = ActiveSheet.Range(Cells(xpH, 1), Cells(xpH, lCol))
'rest of code
 
Upvote 0
The reason I have to code it like this is because I'm not sure where the values will be. I have a sheet with multiple rows that must undergo their own conditional formatting similar to what is being done here. The trick is that I don't know where the rows and their values will end up because code proceeding this can modify/delete rows of empty data.

Your code seems to work good! Now to apply it to multiple other rows and see whats happens.

Thanks very much.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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