WHY Wont ThiS WoRk Somone Anyone Help !

D

Dan Thompson

I have used this line of code many times in programs in order to get VBA to
select a row or multiple rows here is the code.

Rows("10:10").Select 'This will select row 10
Or
Rows("10:50").Select 'This will select rows 10 thru 50

But when I use a variable for the Integers in the two lines of code above I
get errors.
for example.
Sub Test()
Dim X As Integer, Y As Integer
X = 10
Y = 10
With Worksheets(1)
Rows("X:Y").Select '*****THIS LINE PRODUCES AN ERROR Why ?
Rows(X:Y).Select '******THIS LINE ALSO PRODUCES AN ERROR Why ?

End Sub

Any HELP on this would be very much Appreciated

Thanks,
Dan Thompson
 
D

Dave Peterson

Another one...

Sub Test()
Dim X As Long
Dim Y As Long
X = 10
Y = 12
With Worksheets(1)
.Rows(x).resize(y-x+1).Select
End with

End Sub
 
R

Ron Rosenfeld

I have used this line of code many times in programs in order to get VBA to
select a row or multiple rows here is the code.

Rows("10:10").Select 'This will select row 10
Or
Rows("10:50").Select 'This will select rows 10 thru 50

But when I use a variable for the Integers in the two lines of code above I
get errors.
for example.
Sub Test()
Dim X As Integer, Y As Integer
X = 10
Y = 10
With Worksheets(1)
Rows("X:Y").Select '*****THIS LINE PRODUCES AN ERROR Why ?

"X:Y" returns the literal string "X:Y" which is meaningless to the Rows
command. Neither VBA nor Excel will substitute variables within a string like
that.

If you want to return an expression with the variables substituted, you need
something like

X & ":" & Y
Rows(X:Y).Select '******THIS LINE ALSO PRODUCES AN ERROR Why ?

X:Y would be like writing the number 10:10 and NOT the string "10:10"

The colon :)) (outside of a string) in VBA has at least two uses of which I am
aware. It is required to follow a line label; and it can be used to separate
multiple statements on one line.



End Sub

Any HELP on this would be very much Appreciated

Thanks,
Dan Thompson

--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top