Simple Question but I can't seem to find the answer!!

Overkill32

New Member
Joined
May 13, 2011
Messages
49
I want to select a range of entire rows (for exemple Range("1:2").select) with whom i can identify the rows using a variable (for the purpose of the question, let's call the variables Row1 and Row2). So, if I was to decide that Row1=5 and Row2=9, I want to know how i can code the selection of the range using only the variable that i created .

Thank you in advance to whoever can answer me!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
You want to declare row variables as Long

Code:
    Dim iRow1 As Long
    Dim iRow2 As Long
    
    iRow1 = 7
    iRow2 = 5
    
    Range(Cells(iRow1, "A"), Cells(iRow2, "A")).EntireRow.Select
    ' or 
    Rows(iRow1 & ":" & iRow2).Select
 
Upvote 0
With Integer you can't go past 32768 rows (2^15).
 
Upvote 0
Not related to the original question I know but..
I've pretty much given up using Integer at all. From http://msdn.microsoft.com/en-us/library/aa164754(v=office.10).aspx
In recent versions, however, VBA converts all integer values to type Long, even if they are declared as type Integer. Therefore, there is no longer a performance advantage to using Integer variables; in fact, Long variables might be slightly faster because VBA does not have to convert them.
 
Upvote 0
Agree, Peter.

And after converting to Long, they must have additional overhead in converting back and checking for overflow.
 
Upvote 0
Thanks to all the quick answers! I didn't know how to structure the Range syntax to incorporate variables instead of direct references. This will surely make it easier for me!
 
Upvote 0

Forum statistics

Threads
1,224,608
Messages
6,179,872
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top