Test to see if selected range is single row?

D

davegb

I have a macro that asks the user to highlight the first row of data
in the worksheet so it can find and separate the worksheet into
several smaller sheets based on the identifier in the first column. I
want to be sure that the user has only selected a single row in the
input box before the rest of the macro executes. I could write code to
get the row number for the top row of the selected range and the row
number for the last row in the selected range and compare them, but
this seems more complicated than it needs to be. Is there an easier
way? Some kind of simple IF statement that tells the user to try again
if there's more than one row in their selection?

Thanks!
 
G

Guest

If selection.Rows.Count>1 then
Msgbox "Select only one row"
Else
'process the selected row
End if
 
D

Dave Peterson

Instead of yelling at the user if he/she selected a range with multiple rows,
you could just take the top row:

dim myRng as range
set myrng = nothing
on error resume next
set myrng _
= application.inputbox(Prompt:="select a range",type:=8).areas(1).rows(1)
on error goto 0
if myrng is nothing then
'user hit cancel
else
'do what you want
end if

Or just the first cell -- or the entire row of the first cell -- or anything you
want.
 
D

davegb

Instead of yelling at the user if he/she selected a range with multiple rows,
you could just take the top row:

Actually, I wasn't going to yell at them... :)

Interesting point. I just need to know that the word "Adams" is
somewhere in the first row of the range they selected. If not, they
need to try again. If so, we're good to go. Think I'll take your
suggestion.
Thanks, Dave!
 

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