ResourcefulDB
asked on
Quick VBA question - defining cell range
Experts,
I have quick question in VBA programming. How do we select cell ranges when both the row range and column range are variables whose values are obtained from the program before it.
For example, if we want to auto fit ranges from B2 to E7, the code will be
range("B2:E7").autofit.
However, the indices could change for the next worksheet we apply the code to. It could change to, say, C3:D8. The variable keep track of those row and column indices are ci, cj for begin and end column, ri, rj for begin and end row. Now that, if we do know ci, cj, ri, rj, what will be the code look like?
Will it be
range(indirect("ciri,cjrj" )) ? it does not seem to work.
Thanks
RDB
I have quick question in VBA programming. How do we select cell ranges when both the row range and column range are variables whose values are obtained from the program before it.
For example, if we want to auto fit ranges from B2 to E7, the code will be
range("B2:E7").autofit.
However, the indices could change for the next worksheet we apply the code to. It could change to, say, C3:D8. The variable keep track of those row and column indices are ci, cj for begin and end column, ri, rj for begin and end row. Now that, if we do know ci, cj, ri, rj, what will be the code look like?
Will it be
range(indirect("ciri,cjrj"
Thanks
RDB
ASKER
Got it.
With this syntax, if we want to change the selected range's both font and background to white, is the following the right syntax?
Range(Cells(ri, ci), cells(rj, cj)).selection
With Selection.Font
.ColorIndex = 2
End With
With Selection.Interior
.ColorIndex = 2
End With
the vba does not seem to like the line
Range(Cells(ri, ci), cells(rj, cj)).selection
what could be an alternative?
Thanks,
RDB
With this syntax, if we want to change the selected range's both font and background to white, is the following the right syntax?
Range(Cells(ri, ci), cells(rj, cj)).selection
With Selection.Font
.ColorIndex = 2
End With
With Selection.Interior
.ColorIndex = 2
End With
the vba does not seem to like the line
Range(Cells(ri, ci), cells(rj, cj)).selection
what could be an alternative?
Thanks,
RDB
No need to select (you would need Select rather than Selection) - just use:
With Range(Cells(ri, ci), cells(rj, cj))
.Font.ColorIndex = 2
.Interior.ColorIndex = 2
End With
Regards,
Rory
With Range(Cells(ri, ci), cells(rj, cj))
.Font.ColorIndex = 2
.Interior.ColorIndex = 2
End With
Regards,
Rory
ASKER
Rory,
Another quick question follow up. Should the variables in cells be numericals? for example, for cell c4, should it be cells(4,3) since the row for c4 is 4 and column for c4 is 3?
The question arise when the following works
DestWs.Range("A1:G10").Sel ect
Selection.ClearContents
but not this one
DestWs.Range(Cells(1, 1), Cells(10, 7)).Select
Selection.ClearContents
Thanks
RDB
Another quick question follow up. Should the variables in cells be numericals? for example, for cell c4, should it be cells(4,3) since the row for c4 is 4 and column for c4 is 3?
The question arise when the following works
DestWs.Range("A1:G10").Sel
Selection.ClearContents
but not this one
DestWs.Range(Cells(1, 1), Cells(10, 7)).Select
Selection.ClearContents
Thanks
RDB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Range(Cells(ri, ci), cells(rj, cj))
Regards,
Rory