Command button

J

JeffK

I want to add a command button in the first cell of each row (A11:A500) that
deletes the entire row when it's selected by the user. It would be
appreciated if someone could help. I consider myself a newbie so step by
step instructions would be usefull.

thanks
 
R

ryguy7272

Is this something that you can work with?
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
Dim C As Range
Cancel = True
If C.Value = "" Then
C.EntireRow.Delete
End If
End Sub

Or this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
Dim C As Range
Dim refrange As Range
Set refrange = Sheets("Sheet1").Range("A1:A500")
For Each C In refrange
If C.Value = "" Then
C.EntireRow.Delete
End If
Next
End Sub

Regards,
Ryan---
 
J

JLGWhiz

A user can delete a row by clicking on the row number, then Edit>Delete.
Having said that, you do not need a button for each row if you are going to
allow the user to determine which row to delete. You will need only one
button.

Scenario 1:

If you create your button from the forms tool bar, you will need this macro.

Sub delRow()
Selection.EntireRow.Delete
End Sub

Copy this macro into your VBE code window for module1. Then, when you
create your button, a dialog box will appear for you to attach the macro to
the button. Save the file. The user can then select a cell in any row that
they want to delete and click the button.

Scenario 2:

If you create your button from the Control Tool Box toolbar, you will need
this macro.

Private Sub CommandButton1_Click()
Selection.EntireRow.Delete
End Sub

When you create your button, you will need to right click the button while
still in design mode (you will see the sizing nodes) to display the pop up
menu. Select View Code from that menu, then when the code window opens,
paste the above code into it. Close the Code window, click the design mode
icon on the Tool Box to exit design mode and save the file. You are now
ready to delete rows with the button the same as the other one.

If you want to put captions on the buttons, consult the help files.
 

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