Variable rows

J

Jwil

Hello,

How do I use a variable to select entire rows? I want to ask the user how
many rows they want to select and then use that number to select the rows.

For instance:
ActiveCell.Offset(0, 0).Rows("1:58").EntireRow.Select
selects 58 rows. I would like to replace the '58' with a variable so that I
can choose how many I want to delete.
 
K

ker_01

Sub AircodeSampleWarning

MyUserinput = Inputbox("Please enter a number")
ActiveCell.Offset(0, 0).Rows("1:" & MyUserInput).EntireRow.Select

End Sub

You may want to consider checking the user input to make sure that it is a
valid entry before executing your selection. If they enter "sfghsg" the code
will error out when trying to select that range.

I could also see the rare but unfortunate instance where a user meant to
type "9" but accidently hits the key twice and deletes 99 rows. Perhaps
consider selecting the rows, then give the user a chance to abort if they
were to hasty and mis-entered their number of rows to delete...

HTH
Keith
 
R

Rick Rothstein

I would use this instead of the complex statement you are using...

Rws = 58
ActiveCell.Resize(Rws).Select

where I used a variable named Rws to hold the number of rows down from the
active cell to select.
 
R

Rick Rothstein

Whoops! I forgot the EntireRow part...

ActiveCell.Resize(V).EntireRow.Select
 
J

Jwil

Thanks a lot.

ker_01 said:
Sub AircodeSampleWarning

MyUserinput = Inputbox("Please enter a number")
ActiveCell.Offset(0, 0).Rows("1:" & MyUserInput).EntireRow.Select

End Sub

You may want to consider checking the user input to make sure that it is a
valid entry before executing your selection. If they enter "sfghsg" the code
will error out when trying to select that range.

I could also see the rare but unfortunate instance where a user meant to
type "9" but accidently hits the key twice and deletes 99 rows. Perhaps
consider selecting the rows, then give the user a chance to abort if they
were to hasty and mis-entered their number of rows to delete...

HTH
Keith
 

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