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
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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,887
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!!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,651
Messages
5,854,954
Members
431,689
Latest member
jacker01

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