Range ("1:7") With Variables?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How would I express Range ("1:7") by replacing the 1 and the 7 with integer
variables?
 
One simple way (I'm sure other people will present other ways) is:
Range(var1 & ":" & var2)
 
This demonstrates another way to do this with Excel objects:

Option Explicit

Public Sub RowSelect(WS As Worksheet, intRow1 As Integer, intRow2 As Integer)
Dim rngTest As Range
With WS
Set rngTest = .Range(.Rows(intRow1), .Rows(intRow2))
End With
rngTest.Select
End Sub

Public Sub TestRowSelect()
RowSelect WS:=ThisWorkbook.Worksheets(1), intRow1:=1, intRow2:=7
End Sub

Bob
 
My preference is to avoid strings if possible.

Sub Demo()
Dim r1 As Long
Dim r2 As Long

r1 = 1
r2 = 7
Rows(r1).Resize(r2 - r1 + 1).Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top