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).EntireR ow
which executes, but then this:
ws.range(ws.cells(1,1),ws. cells(500, 1).EntireR ow.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!
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.
which executes, but then this:
ws.range(ws.cells(1,1),ws.
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!
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..
Saurabh...
dim sr as long,er as long
sr=1
er=500
myrange=ws.rows(sr &":"& er)
Saurabh...
You are missing a right parenthesis character.
ws.range(ws.cells(1,1),ws.cells(500, 1).EntireR ow.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.
ws.range(ws.cells(1,1),ws.cells(500,1)).EntireRow.Select
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?
@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...
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...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And thanks for the help on this!
ASKER
I included my comment as part of the solution because it summarized the situation.
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...
Open in new window
or
Open in new window
Saurabh...