excel vba select range of row with numbers only

codequest
codequest used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
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...

Author

Commented:
Thanks for the input.     Pardon, I wan't specific....I'm trying to avoid ALL string constructs, including "1:500"  (if possible)
Top Expert 2015

Commented:
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...
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Top Expert 2014

Commented:

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

Author

Commented:
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?
Top Expert 2015

Commented:
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...

Author

Commented:
@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.
Top Expert 2014
Commented:
are your service patches up to date?
I invoked your select method on an Excel 2003 workbook and it worked as expected.
Most Valuable Expert 2011
Top Expert 2011
Commented:
You have to select the worksheet first before you can select the range.
@Rory:   That was it.   Yep, must do ws.Activate or other selection of the worksheet.  Then this construct works:

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

Author

Commented:
And thanks for the help on this!

Author

Commented:
I included my comment as part of the solution because it summarized the situation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial