none
Find first empty row in table? RRS feed

  • Question

  • My document contains one big table in the middle, which initially contains 25 rows and 6 columns.  (If the table becomes full, users will typically add extra rows, so it is possible the row count can grow.  The column count is always fixed)

    Starting with row 1 of the table, I would like to search all rows in the table and find the first empty row.  I consider a row to be empty if each of the cells in that row are empty or contain nothing but whitespace characters.  Once the first empty row is found, I want to write the word "EMPTY" in each cell for that row.  Finally, if the table is full (ie has no empty rows), then I want to display a message box that says "This table is full".

    Can anyone help with making this macro?  I'm familiar with Excel VBA, but I'm really a newbie with Word VBA.

    Thank you!

    Robert C.

     


    jmmc
    Sunday, March 6, 2011 4:12 AM

Answers

  • The following should work if the cells are empty - though I would need some
    clarification on what you mean by 'white space characters' to take account
    of whatever they might be in this context.
     
    Dim oTable As Table
    Dim oRow As Row
    Dim i As Long, j As Long
    Dim Count As Long
    Set oTable = ActiveDocument.Tables(1)
    For i = 1 To oTable.Rows.Count
        Set oRow = oTable.Rows(i)
        Count = 0
        For j = 1 To 6
            Count = Count + Len(oRow.Cells(j).Range)
        Next j
        If Count = 12 Then
            For j = 1 To 6
                oRow.Cells(j).Range.Text = "Empty"
            Next j
            Exit For
        End If
    If i = oTable.Rows.Count Then MsgBox "Table Full"
    Next i
     
    --
     
     
     
     

    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Sunday, March 6, 2011 3:23 PM

All replies

  • Hello Robert,

    Please read this post: http://www.eggheadcafe.com/software/aspnet/35886615/find-first-empty-row-of-a-table.aspx.


    Cornel Croitoriu - Senior Software Developer & Entrepreneur

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    CWS SoftwareBiz-Forward.comCroitoriu.NET

    Sunday, March 6, 2011 2:33 PM
  • The following should work if the cells are empty - though I would need some
    clarification on what you mean by 'white space characters' to take account
    of whatever they might be in this context.
     
    Dim oTable As Table
    Dim oRow As Row
    Dim i As Long, j As Long
    Dim Count As Long
    Set oTable = ActiveDocument.Tables(1)
    For i = 1 To oTable.Rows.Count
        Set oRow = oTable.Rows(i)
        Count = 0
        For j = 1 To 6
            Count = Count + Len(oRow.Cells(j).Range)
        Next j
        If Count = 12 Then
            For j = 1 To 6
                oRow.Cells(j).Range.Text = "Empty"
            Next j
            Exit For
        End If
    If i = oTable.Rows.Count Then MsgBox "Table Full"
    Next i
     
    --
     
     
     
     

    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Sunday, March 6, 2011 3:23 PM