Get last column for indirect formula

coop123

Board Regular
Joined
Dec 18, 2018
Messages
66
Office Version
  1. 365
Hello

I currrently have this section of a macro which works perfectly, my problem is that the last column changes each time the report is run.
In the case below last column is CO hence indirect formula =INDIRECT(""CO""&ROW())>0"
Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=INDIRECT(""CO""&ROW())>0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
I have tried to set last column but i am not sure on how to enter it into INDIRECT formula. see below.
Dim lc As Long
lc = Cells(3, Columns.Count).End(xlToLeft).Column

Range("A3").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=INDIRECT(lc&ROW())>0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

any advice would be very much appreciated.

Thanks in advance

coop123
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I see no reason for using INDIRECT here, as with many questions, it is better if you explain what you're trying to do rather than how you're trying to do it.

See if this works as needed.
VBA Code:
Sub coop123()
Dim lc As Long, lr As Long
lc = Cells(3, Columns.Count).End(xlToLeft).Column
lr = Cells(Rows.Count, lc).End(xlUp).Row

With Range("A3", Cells(lr, lc))
    .FormatConditions.Add Type:=xlExpression, Formula1:="=" & Cells(3, lc).Address(0, 1) & ">0"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1)
        With .Interior
            .PatternColorIndex = xlAutomatic
            .Color = 13551615
            .TintAndShade = 0
        End With
        .StopIfTrue = False
    End With
End With
End Sub
 
Upvote 0
Solution
Hi jasonb75

Thank you your solution works perfectly.

I am highlighting the entire row where the value in the end column of that row has a value greater than zero.

Thanks again

coop123
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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