Code help - deleting lines from a table

S

sjs1967

I am trying to create a template that consists of a table of 2 columns and a
variable amount of rows, depending upon the userform responses. For
instance, if 6 checkboxes are checked on the userform, there would be 6 rows
in one section of the table with text in them (obtained from the checkbox).
If 3 boxes are checked, 3 rows, etc. Can anyone help? Thanks
 
D

Doug Robbins - Word MVP

To delete a row from a table you would use

ActiveDocumnet.Tables(i).Rows(j).Delete

You would need to tell us more about how the checkboxes relate to the
individual rows to give you something more specific.

--
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
 
S

sjs1967

Thank you for the start Doug. The checkboxes relate to the individual rows
on a one-to-one basis (for now - there may be a better way), sending test to
bookmarks. In row 7, col 1, I have text saying "inclusions". In column 2,
rows 7-16, I would like to list the "inclusions" in the table if the user has
checked the corresponding boxes on the userform, but delete rows if all the
boxes are not checked. So if the user checks 5 boxes, there would only be 5
rows in that section of the table. I imagine it will involve if..then
statements, where:
if checkbox1 is false then activedocument.table1.row6.delete?
 
S

sjs1967

Well, I found some code that does what I like:

Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean

' Specify which table you want to work on.
Set oTable = Selection.Tables(1)
' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False

For Counter = 1 To NumRows

StatusBar = "Row " & Counter
TextInRow = False

For Each oCell In oRow.Rows(1).Cells
If Len(oCell.Range.Text) > 2 Then
'end of cell marker is actually 2 characters
TextInRow = True
Exit For
End If
Next oCell

If TextInRow Then
Set oRow = oRow.Next(wdRow)
Else
oRow.Rows(1).Delete
End If

Next Counter

Application.ScreenUpdating = True


but, how does one get the cursor in a cell automatically? My user form
loads info into a table, and if I manually click in a table cell the above
code works, but how do I load the user form info into the table and then
choose a cell automatically for the above to work automatically?
 

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