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!
Microsoft Excel

Avatar of undefined
Last Comment
codequest

8/22/2022 - Mon
Saurabh Singh Teotia

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...
codequest

ASKER
Thanks for the input.     Pardon, I wan't specific....I'm trying to avoid ALL string constructs, including "1:500"  (if possible)
Saurabh Singh Teotia

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...
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
aikimark


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

codequest

ASKER
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 Teotia

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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
codequest

ASKER
@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
aikimark

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Rory Archibald

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
codequest

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
codequest

ASKER
And thanks for the help on this!
codequest

ASKER
I included my comment as part of the solution because it summarized the situation.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck