Detect if current select is a row

B

Billy B

Is it possible to test to see if the current selection is a row? I have a
command button that is designed to delete the selected row but but I need to
test to see if it is selected before running the rest of the macro.

Thank you.
 
R

Rick Rothstein

Why test if the entire row is selected when you can force the selection of
the entire row; something like this...

' Ask user to select the row as you do now, then run this code
If Selection.Columns = 1 Then
Selection.EntireRow.Select
'
' the rest of your code goes here
'
Else
MsgBox "You selected more than one row!"
End If

Although I'm willing to bet that you don't really need to perform the
selection operation as opposed to simply working with the range itself in
code.
 
B

Billy B

Thank you for the help. What is really going on is the user may or may not
select the row and if the row is selected, do the procedure and if only a
cell is selected, select the row. The next reply did address that. Thanks
again.
 
T

Tim Zych

Other options, customize depending on how much control you want to have, or
to what degree you want to flex based on the user selection.

' To accept the top left cell as the starting point, but expand to the
entire row. One row max.
Selection.Cells(1,1).EntireRow.Delete

' To allow multiple rows to be deleted at one time
Selection.EntireRow.Delete

' To force the user to re-select
With Selection
If .Rows.Count > 1 Or .Columns.Count < Columns.Count Then
MsgBox "Select one row to delete from."
Else
.EntireRow.Delete
End If
End With
 
R

Rick Rothstein

If Selection.Columns = 1 Then

Of course the above should have been...

If Selection.Rows = 1 Then
 

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