Determine row number of command button on click

R

Robert S.

Hi

I have a tabe and on each line there is a command button. Once the user clicks on the button, I want that row to be deleted. Since I do not know how many rows the table will have beforehand, each row will have the exact same button and script

I need to have the script determine which row the button that was clicked was on. Anyone know how to do this

Also, I know I can used check boxes at the end of each row and use one button to delete the checked rows. This is not ideal for my situation, so any help on former would be very much appreciated

Thanks.
 
D

Dave Lett

Hi Robert,

If you want to delete the current row, then you can use the following:

Selection.Rows(1).delete

HTH,
Dave

Robert S. said:
Hi,

I have a tabe and on each line there is a command button. Once the user
clicks on the button, I want that row to be deleted. Since I do not know
how many rows the table will have beforehand, each row will have the exact
same button and script.
I need to have the script determine which row the button that was clicked
was on. Anyone know how to do this?
Also, I know I can used check boxes at the end of each row and use one
button to delete the checked rows. This is not ideal for my situation, so
any help on former would be very much appreciated.
 
J

Jay Freedman

Hi Robert

If you're using command buttons from the Control Toolbox, you can use code
like this in the ThisDocument module. Note that each button *must* have its
own _Click() procedure, but they can all point to the same routine.

Private Sub CommandButton1_Click()
ButtonAction
End Sub

Private Sub CommandButton2_Click()
ButtonAction
End Sub

Private Sub CommandButton3_Click()
ButtonAction
End Sub
Private Sub ButtonAction()
Dim oThisRow As Row
With Selection.Tables(1)
For Each oThisRow In .Rows
If Selection.InRange(oThisRow.Range) Then
oThisRow.Delete
Exit For
End If
Next oThisRow
End With
End Sub

If you use MacroButton fields to create the buttons, all you need is the
ButtonAction() procedure (which in this case should go into a regular module
instead of ThisDocument and be declared Public instead of Private), and all
the fields can point to the same place:

{ MacroButton ButtonAction Delete row }

You can use the Format > Font and Format > Borders and Shading dialogs to
make the words "Delete row" look like a command button.
 
R

Robert S.

"Note that each button *must* have its own _Click() procedure, but they can all point to the same routine.

In my table the user is able to add a new row (which includes formfields text inputs) with the click of a button. When the new row is added I did a selection copy and paste of the previous button. But as you said, each button needs its own Click() procedure.

Is there a way to have the program automatically add "ButtonAction" to each new sub created

Thanks
Robert
 
J

Jay Freedman

Technically there is a way -- VBA includes functions for creating code, for
which you need to include a reference to the VBA Extensibility library --
but it's not something I have personal experience with.

That's why I suggested switching to MacroButton fields. When you copy and
paste them, there's no need to alter the macro at all.

Since you're using form fields in the table, I assume you're dealing with a
protected document. In that case, the macro should be

Public Sub ButtonAction()
ActiveDocument.Unprotect
Selection.Rows(1).Delete
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True
End Sub
 

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