Home > OS >  Setting a range from a cell to that cell's row value as the row index and the last column as th
Setting a range from a cell to that cell's row value as the row index and the last column as th

Time:02-25

trying to set a range from RValue to RValue row till last column in that row. I have tried the code below but have been struggling to get any of them to work.

Range(RValue, Cells(RValue.Row, LastColumn.Column))
'or
Range(RValue, Cells(RValue.Row, LastColumn).Column)
'or
Range(RValue:cells(RValue,LastCol))

CodePudding user response:

This is a compressed solution that can error when using on an empty row:

    Set r = Range(RValue, Cells(RValue.Row, Cells(RValue.Row, Columns.Count).End(xlToLeft).Column))
    Debug.Print RValue.Address

And this is a more complex but robust solution:

    Dim RValue As Range, iCol As Long
    
    iCol = Cells(RValue.Row, Columns.Count).End(xlToLeft).Column - RValue.Column   1
    If iCol < 1 Then iCol = 1
    Set RValue = RValue.Resize(1, iCol)
    Debug.Print RValue.Address
  • Related