ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using variables in the Range command (https://www.excelbanter.com/excel-programming/324393-using-variables-range-command.html)

Chi Man Wong

Using variables in the Range command
 
Usually the Range method looks like:
Range("A1:C50")

In my case, the first and last column are variables. How can I still
use the Range method? I need to find a command to define my range
like:

dim rgRange as range

rgRange = sheetname.range( (row i, column j): (row m, column n) )

any help?

RB Smissaert

Using variables in the Range command
 
Sub test()

Dim Row1 As Long
Dim Col1 As Long
Dim Row2 As Long
Dim Col2 As Long
Dim rngRange As Range

Set rngRange = _
Sheets(1).Range(Cells(Row1, Col1), _
Cells(Row2, Col2))

End Sub

RBS


"Chi Man Wong" wrote in message
om...
Usually the Range method looks like:
Range("A1:C50")

In my case, the first and last column are variables. How can I still
use the Range method? I need to find a command to define my range
like:

dim rgRange as range

rgRange = sheetname.range( (row i, column j): (row m, column n) )

any help?



Dave Peterson[_5_]

Using variables in the Range command
 
A minor modification of RBS's solution--just in case the activesheet wasn't
sheets(1):

Sub test()

Dim Row1 As Long
Dim Col1 As Long
Dim Row2 As Long
Dim Col2 As Long
Dim rngRange As Range

col1 = 3
row1 = 1
col2 = 5
row2 = 7

with sheets(1)
Set rngRange = .Range(.Cells(Row1, Col1), .Cells(Row2, Col2))
end with

End Sub

Note the extra dots with the .cells().

Chi Man Wong wrote:

Usually the Range method looks like:
Range("A1:C50")

In my case, the first and last column are variables. How can I still
use the Range method? I need to find a command to define my range
like:

dim rgRange as range

rgRange = sheetname.range( (row i, column j): (row m, column n) )

any help?


--

Dave Peterson


All times are GMT +1. The time now is 01:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com