简体   繁体   中英

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

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))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM