Link to home
Start Free TrialLog in
Avatar of codequest
codequest

asked on

excel vba select range of row with numbers only

In statements like this:

dim ws as worksheet
ws = ThisWorkbook.worksheets(1)

ws .Rows([range of rows]).AutoFit

or

Set myRange =  ws .Rows([range of rows]).EntireRow

how might I specify the [range of rows] using just numbers, instead of the string "R1C4" type construct?

That is, something like this:  

ws.range(ws.cells(1,1),ws.cells(500,1).EntireRow

which executes, but then this:

ws.range(ws.cells(1,1),ws.cells(500,1).EntireRow.Select

fails with "Select method of range failed", so I can't verify if I really have the range of rows I want in r1.

Any suggestions on this would be appreciated.

Thanks!
Avatar of Saurabh Singh Teotia
Saurabh Singh Teotia
Flag of India image

You can do something like this...

Set myrange = ws.Rows("1:500")

Open in new window


This will select row number-1 to row -500 in your my range..Now in order to select as well you can do this...
ws.Range("1:500").Select

Open in new window


or

ws.Rows("1:500").Select

Open in new window


Saurabh...
Avatar of codequest
codequest

ASKER

Thanks for the input.     Pardon, I wan't specific....I'm trying to avoid ALL string constructs, including "1:500"  (if possible)
Assuming your startrow is sr and endrow as er and starts at 1 and ends at 500..you can do something like this and then you can change sr and er to whatever logic you want to use to determine the starting point and end point of your data-set..

dim sr as long,er as long
sr=1
er=500
myrange=ws.rows(sr &":"& er)

Open in new window


Saurabh...

ws.range(ws.cells(1,1),ws.cells(500,1).EntireRow.Select

fails with "Select method of range failed", so I can't verify if I really have the range of rows I want in r1.
You are missing a right parenthesis character.
ws.range(ws.cells(1,1),ws.cells(500,1)).EntireRow.Select

Open in new window

Thanks for the input.

@aikimark: You're correct on the missing right parens...though oddly that's not missing in my code file.    And in my code file it still gives the same error.

ws.Range(ws.Cells(1, 1), ws.Cells(500, 1)).EntireRow.Select

@Saurabh:  with respect to  myrange=ws.rows(sr &":"& er), that's simpler, thoughtstill using the string construct.  

 So, maybe there IS no way to do it purely with numbers, without string usage?
codequest,

Yes if you are using a combination of range you need to use a string construct without it you can't use the entire range...you can just select a single row only..

Saurabh...
@Sauraba:  Thanks, I expect you're correct.  I'm going to let this sit out there for a day and see if there are any other views, then close it out.
SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And thanks for the help on this!
I included my comment as part of the solution because it summarized the situation.