I am trying to use Last column for my range: WS.range("F2:LastCol" & LastRow).Cells
my sub works for
WS.range("F2:K" & LastRow).Cells
but the Last Column
is dynamic and keeps changing
Thanks
Sub QQ()
Dim LastRow As Long
Dim LastCol As Long
Dim WS As Worksheet
Dim rCell As range
Set WS = Sheets("sheet1")
LastRow = WS.range("F" & WS.Rows.Count).End(xlUp).Row
LastCol = Cells(2, .Columns.Count).End(xlToLeft).Column
'Also tried: LastCol=rgRange.Cells(rgRange.Count).Column
For Each rCell In WS.range("F2:LastCol" & LastRow).Cells
STUFF 'The stuff works for WS.range("F2:K" & LastRow).Cells
End Sub
LastCol is a number, and the syntax you're using to specify the range requires a letter.
You can find out the column letter for the column number and pass it in to your range definition like this:
Please try to define your loop this way:
or it could go shorter way with this solution (if
WS
isActivesheet
):