Delete a column with a checkbox

S

sg

I have a table that has 3 columns - sometimes I need the last column and
sometimes I don't. Is it possible to put a formfield checkbox at the top of
the column so that if it is not checked, the column will be deleted?

Likewise, some of the rows may be needed, some may not. Can I also put a
checkbox in front of each row to control which can be deleted and which
cannot?
 
D

Doug Robbins - Word MVP

The problem with doing that sort of thing is that there is no going back if
you change your mind.

The following code run on exit from the checkbox will however give you a
chance to change your mind:

Dim Response
With ActiveDocument
If .FormFields("Check1").CheckBox.Value = True Then
Response = MsgBox("Are you sure that you want to delete the
column?", vbQuestion + vbYesNo)
If Response = vbYes Then
.Unprotect
.Tables(1).Columns(3).Delete
.Protect wdAllowOnlyFormFields, NoReset
End If
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

sg

That is awesome. Thank you!

Doug Robbins - Word MVP said:
The problem with doing that sort of thing is that there is no going back if
you change your mind.

The following code run on exit from the checkbox will however give you a
chance to change your mind:

Dim Response
With ActiveDocument
If .FormFields("Check1").CheckBox.Value = True Then
Response = MsgBox("Are you sure that you want to delete the
column?", vbQuestion + vbYesNo)
If Response = vbYes Then
.Unprotect
.Tables(1).Columns(3).Delete
.Protect wdAllowOnlyFormFields, NoReset
End If
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

sg

If I could bother you again...

I tweaked the code you gave me to also delete a row in the same table if the
checkbox is not checked. Is there a way to specify that it should be the row
that the checkbox is in instead of specifying row 2 or row 3?
 
D

Doug Robbins - Word MVP

Selection.Rows(1).Delete

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

Doug Robbins - Word MVP

You need to use:

Dim Response
With ActiveDocument
If .FormFields("Check1").CheckBox.Value = True Then
Response = MsgBox("Are you sure that you want to delete the
column?", vbQuestion + vbYesNo)
If Response = vbYes Then
.Unprotect
.FormFields("check1").Range.Rows(1).Delete
' .Tables(1).Columns(3).Delete
.Protect wdAllowOnlyFormFields, NoReset
End If
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

sg

Thanks! That is absolutely perfect!!

Doug Robbins - Word MVP said:
You need to use:

Dim Response
With ActiveDocument
If .FormFields("Check1").CheckBox.Value = True Then
Response = MsgBox("Are you sure that you want to delete the
column?", vbQuestion + vbYesNo)
If Response = vbYes Then
.Unprotect
.FormFields("check1").Range.Rows(1).Delete
' .Tables(1).Columns(3).Delete
.Protect wdAllowOnlyFormFields, NoReset
End If
End If
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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