What is wrong with this code?

sizemorem

Board Regular
Joined
Dec 12, 2008
Messages
103
I want to do conditional formatting on my spreadsheet. My labels are on row 7, columns A:E. I want my conditional formatting to start with row 8. It applies the formatting to row 8 but changes the formula to row 9. I don't understand.

Code:
Private Sub ConditionalFormattingForRefundCkReg()
    Selection.FormatConditions.Delete
 
    Range("A8").CurrentRegion.Select
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=IF(AND($E8=""Voided"",$D8>0),TRUE,FALSE)"
    Selection.FormatConditions(1).Interior.ColorIndex = 38
 
End Sub

The formatting that is cell A8 is:
"=IF(AND($E9="Voided",$D9>0),TRUE,FALSE)"

All help is GREATLY appreciated
 

Some videos you may like

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,881
Hi

Code:
Range("A8").[COLOR=blue][B]CurrentRegion[/B][/COLOR].Select

You are selecting the current region and so the labels are also included, making row 8 the second row. That's why you get the 9 in the conditional formatting formula.

You should only select from row 8, down.
 

sizemorem

Board Regular
Joined
Dec 12, 2008
Messages
103
If I use
Code:
Range("A8:E", Cells(Rows.Count, 1).End(xlUp).Row).Select

I get an error message about "Method or Object Failed"

Can you suggest another method of selecting an indeterminate range
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,881
Try:

Code:
Range("A8:E" & Cells(Rows.Count, 1).End(xlUp).Row).Select
 

sizemorem

Board Regular
Joined
Dec 12, 2008
Messages
103
Works terrific now. It is amazing how just small items can mess everything up. I have a LOT to learn.

Thank you so much!!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,554
Messages
5,596,811
Members
414,104
Latest member
imamalidadashzada

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
Top