Results 1 to 6 of 6
  1. #1
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cycle Through Cells in Table (VBA - Word 2002)

    My users are going to be updating totals within a Word document that has 2 tables in it. I need to be able to cycle through Table(2) and grab the most recent value and that value will be placed in a database. So, the first column has a heading and then the subsequent cells to the right of that are where the values are placed. The values are not overwritten, the new ones are just placed in the next cell along. There is not going to be a set number of revisions and the table may go for more than one page. I know how to cycle through and place text, but I can't seem to get my mind wrapped around cycling through until the most recent value (the far right-most cell with something in it) is found and then capture it. Any help is most appreciated! Thanks.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cycle Through Cells in Table (VBA - Word 2002)

    The following macro will retrieve the last filled-in cell in each row. As an example, the text is shown in a message box. Replace that with your own code.

    Sub LoopTable()
    Dim tbl As Table
    Dim r As Integer
    Dim c As Integer
    Dim strVal As String
    Dim strPrev As String
    Set tbl = ActiveDocument.Tables(2)
    For r = 1 To tbl.Rows.Count
    strPrev = ""
    For c = 2 To tbl.Columns.Count
    ' Get text of cell
    strVal = tbl.Cell(r, c).Range.Text
    ' Remove end markers
    strVal = Trim(Left(strVal, Len(strVal) - 2))
    If strVal = "" Then
    Exit For
    Else
    strPrev = strVal
    End If
    Next c
    MsgBox "Row " & r & ": " & strPrev
    Next r
    End Sub

    Notes:
    1. The above code will fail if there are merged cells in the table.
    2. I would do this in Excel instead of Word.

  3. #3
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cycle Through Cells in Table (VBA - Word 2002)

    Thanks, Hans. I will try this on Monday as it is work-related. I agree with you that Excel is probably a better place for this kind of activity; however, this is especially tied to a Word printout that lives in client files and is updated periodically. Also, I did note that because it also has merged cells, that I had already had to deal with my attempts breaking because of that. I appreciate your help so very much!! Thanks.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cycle Through Cells in Table (VBA - Word 2002)

    If you have horizontally merged cells, you'll have to change

    For c = 2 To tbl.Columns.Count

    to

    For c = 2 To tbl.Rows®.Cells.Count

    If you also have vertically merged cells, it becomes a mess.

  5. #5
    Star Lounger
    Join Date
    May 2005
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cycle Through Cells in Table (VBA - Word 2002)

    Frankly, I have decided to rework that section of the document so that there are no merged cells in either plane to deal with. [img]/forums/images/smilies/ohmy.gif[/img])

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cycle Through Cells in Table (VBA - Word 2002)

    Great, a wise decision! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •