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!
LVL 2
codequestAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
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...
codequestAuthor Commented:
Thanks for the input.     Pardon, I wan't specific....I'm trying to avoid ALL string constructs, including "1:500"  (if possible)
Saurabh Singh TeotiaCommented:
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...
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

aikimarkCommented:

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

codequestAuthor 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?
Saurabh Singh TeotiaCommented:
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...
codequestAuthor 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.
aikimarkCommented:
are your service patches up to date?
I invoked your select method on an Excel 2003 workbook and it worked as expected.
Rory ArchibaldCommented:
You have to select the worksheet first before you can select the range.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codequestAuthor Commented:
@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
codequestAuthor Commented:
And thanks for the help on this!
codequestAuthor Commented:
I included my comment as part of the solution because it summarized the situation.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.