Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


What I have tried:

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

Both do not work
Posted
Updated 23-Feb-22 4:23am
v4
Comments
Richard MacCutchan 23-Feb-22 3:28am    
As I suggested yesterday, use the macro debugger to find out why those references are not working.
CHill60 23-Feb-22 5:24am    
"do not work" is not a helpful description. Explain precisely what is or is not happening

Of the things you have allegedly tried...
VB
Range(RValue, Cells(RValue.Row, LastColumn).Column)
gives a Type Mismatch error, assuming that LastColumn is a range
VB
Range(RValue:cells(RValue,LastCol))
just gives a compile error because of the colon :

If you want to include a colon in a range definition then use it within a string e.g.
VB
Dim x As Range
Set x = Range(RValue.Address & ":" & LastColumn.Address)
You are actually in the right space with
VB
Range(RValue, Cells(RValue.Row, LastColumn.Column))
What makes you think that it doesn't work?
VB
Sub demo()

    'The "source"
    Dim RValue As Range
    Set RValue = ThisWorkbook.Sheets(1).Range("A7")

    'The first cell in the last column used
    'This can be determined in a number of ways
    Dim LastColumn As Range
    Set LastColumn = ThisWorkbook.Sheets(1).Range("E1")

    'Replicate the Value in Range RValue through from RValue to the Last Column on that row
    ThisWorkbook.Sheets(1).Range(RValue, ThisWorkbook.Sheets(1).Cells(RValue.Row, LastColumn.Column)).Value = RValue.Value

End Sub
Alternatively you could have used OffSet
VB
ThisWorkbook.Sheets(1).Range(RValue, RValue.Offset(0, LastColumn.Column - 1)).Value = RValue.Value


I will also repeat my advice about being explicit about ranges - don't just use Range, that applies to the Active sheet in the Active workbook - and that can easily change with a single click of the mouse - Users will do that. Be explicit - state which Workbook, and which Sheet, or use Withe.g.(Note the periods before Range and Cells!!)
VB
With ThisWorkbook.Sheets(1)
    .Range(RValue, .Cells(RValue.Row, LastColumn.Column)).Value = RValue.Value
End With
 
Share this answer
 
v2
Comments
Maciej Los 23-Feb-22 10:09am    
5ed!
Assuming that RValue and LastColumn is the range object (because you are using .Row and .Column property of these variables), then the simplest way to define another range is to use .Range(RValue, LastColumn). See:

VBA
Dim RValue As Range
Dim LastColumn As Range
Dim FinalRng As Range

Set RValue = ThisWorkbook.Worksheets(1).Range("A1")
Set LastColumn = ThisWorkbook.Worksheets(1).Range("AC1")
Set FinalRng = ThisWorkbook.Worksheets(1).Range(RValue, LastColumn)
Debug.Print FinalRng.Address
'prints: $A$1:$AC$1


Good luck!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900