Rookie: Can't select row with user input?

RicoJ

New Member
Joined
Oct 28, 2017
Messages
5
Hi all,

I am new to Excel and have encounter such problem:

I am trying to prompt the user for rows selection. I am not sure if the variables are set up correctly. I tried with integers but didn't work. Thank you for your help in advance!

The code goes as follow:


Sub Select_row()
Dim leftrow As Variant, rightrow As Variant
leftrow = InputBox("give me your left row")
rightrow = InputBox("Give me your right row")


Range("leftrow:rightrow").Select
End Sub



The error message goes as:


[FONT=&quot]Run-time error '1004':[/FONT]
[FONT=&quot]
[/FONT]
[FONT=&quot]Method 'Range' of object '_Global' failed[/FONT]
<attachment></attachment>
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
The Inputbox function returns a string so you don't need the quote marks. Assuming you expect the user to enter integer values for the row numbers, try:
Code:
Sub Select_row()
Dim leftrow As Variant, rightrow As Variant
leftrow = InputBox("give me your left row")
rightrow = InputBox("Give me your right row")
Rows(leftrow & ":" & rightrow).Select
End Sub
Row numbers increase from top to bottom of an Excel sheet, while column numbers increase from left to right so you variables' terminology is a bit confusing.
 
Upvote 0
Are your trying to select an entire row?

Like:

Rows(12).Select

Never heard or a right and left row.
 
Last edited:
Upvote 0
Hi Joe,

It worked! but what is & ":" & as in Rows(toprow & ":" & bottomrow).Select? Thanks!

Rico
 
Upvote 0
Toprow and bottomrow are already strings b/c they were returned by the Input function. To create the range of rows you want to select they must be separated by a string colon (:). The & ":" & does that. The & acts like the + sign in adding numbers together, but it joins 2 strings instead. So, "A" & "B" creates the string "AB".
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top