Deleting cells with VBA

Discussion in 'Word VBA Beginners' started by Morales, Apr 24, 2007.

  1. Morales

    Morales Guest

    Hi All, I have already posted this message once, but I can't see it.
    If it pops up later - apologies.
    Can any of you gurus tell me how to select and delete a row of cells
    using VBa if the row is empty. In most cases the tables will be one
    cell deep by two cells wide. Thanks in advance.
     
    Morales, Apr 24, 2007
    #1
    1. Advertisements

  2. Morales

    macropod Guest

    Hi Morales,

    The following code will delete all blank rows (except for those with vertically merged cells) in all tables in the document:

    Sub DelBlankRows()
    Dim Pwd As String
    Dim oRow As Integer
    Dim oTable As Table
    With ActiveDocument
    If .Tables.Count > 0 Then
    For Each oTable In .Tables
    For oRow = oTable.Rows.Count To 1 Step -1
    If Len(Replace(oTable.Rows(oRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
    On Error Resume Next 'skip vertically merged cells
    oTable.Rows(oRow).Delete
    End If
    Next oRow
    Next oTable
    End If
    End With
    End Sub

    Cheers
    --
    macropod
    [MVP - Microsoft Word]
    -------------------------

    "Morales" <> wrote in message news:...
    > Hi All, I have already posted this message once, but I can't see it.
    > If it pops up later - apologies.
    > Can any of you gurus tell me how to select and delete a row of cells
    > using VBa if the row is empty. In most cases the tables will be one
    > cell deep by two cells wide. Thanks in advance.
    >
     
    macropod, Apr 24, 2007
    #2
    1. Advertisements

  3. Morales

    macropod Guest

    You can delete 'Dim Pwd As String' - it was left over from another project.

    Cheers
    --
    macropod
    [MVP - Microsoft Word]
    -------------------------

    "macropod" <> wrote in message news:...
    > Hi Morales,
    >
    > The following code will delete all blank rows (except for those with vertically merged cells) in all tables in the document:
    >
    > Sub DelBlankRows()
    > Dim Pwd As String
    > Dim oRow As Integer
    > Dim oTable As Table
    > With ActiveDocument
    > If .Tables.Count > 0 Then
    > For Each oTable In .Tables
    > For oRow = oTable.Rows.Count To 1 Step -1
    > If Len(Replace(oTable.Rows(oRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then
    > On Error Resume Next 'skip vertically merged cells
    > oTable.Rows(oRow).Delete
    > End If
    > Next oRow
    > Next oTable
    > End If
    > End With
    > End Sub
    >
    > Cheers
    > --
    > macropod
    > [MVP - Microsoft Word]
    > -------------------------
    >
    > "Morales" <> wrote in message news:...
    >> Hi All, I have already posted this message once, but I can't see it.
    >> If it pops up later - apologies.
    >> Can any of you gurus tell me how to select and delete a row of cells
    >> using VBa if the row is empty. In most cases the tables will be one
    >> cell deep by two cells wide. Thanks in advance.
    >>
     
    macropod, Apr 24, 2007
    #3
  4. Morales

    Greg Maxey Guest

    Something like this:

    Sub ScratchMacro()
    Dim oTbl As Word.Table
    Dim bEmpty As Boolean
    Dim oRow As Word.Row
    Dim oCell As Word.Cell
    For Each oTbl In ActiveDocument.Range.Tables
    For Each oRow In oTbl.Rows
    bEmpty = True
    For Each oCell In oRow.Cells
    If Len(oCell.Range.Text) > 2 Then bEmpty = False
    Next oCell
    If bEmpty Then oRow.Delete
    Next oRow
    Next oTbl
    End Sub


    On Apr 24, 7:37 am, Morales <> wrote:
    > Hi All, I have already posted this message once, but I can't see it.
    > If it pops up later - apologies.
    > Can any of you gurus tell me how to select and delete a row of cells
    > using VBa if the row is empty. In most cases the tables will be one
    > cell deep by two cells wide. Thanks in advance.
     
    Greg Maxey, Apr 24, 2007
    #4
  5. Morales

    tajmadhu

    Joined:
    Sep 10, 2012
    Messages:
    1
    Likes Received:
    0
    In my word document I have tables in which cells are vertically merged. Could you please let me know how to delete empty rows of such tables?
     
    tajmadhu, Sep 10, 2012
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Schorrie

    deleting text with commando

    Schorrie, Jul 28, 2003, in forum: Word VBA Beginners
    Replies:
    2
    Views:
    180
    Doug Robbins - Word MVP
    Jul 28, 2003
  2. Marcus

    Deleting Lines in Word using VBA

    Marcus, Aug 21, 2003, in forum: Word VBA Beginners
    Replies:
    3
    Views:
    243
    Peter Jamieson
    Aug 22, 2003
  3. Rick

    Deleting a file

    Rick, Oct 22, 2003, in forum: Word VBA Beginners
    Replies:
    5
    Views:
    94
    Charles Kenyon
    Oct 24, 2003
  4. Steve

    Deleting table rows in a protected document

    Steve, Nov 14, 2003, in forum: Word VBA Beginners
    Replies:
    1
    Views:
    154
    Doug Robbins - Word MVP
    Nov 14, 2003
  5. GUNNAR BAARDSEN

    Removing/deleting button from document

    GUNNAR BAARDSEN, Nov 17, 2003, in forum: Word VBA Beginners
    Replies:
    5
    Views:
    178
    Gunnar
    Dec 2, 2003
  6. Stefanos Blairos

    Deleting a project in Word VBA

    Stefanos Blairos, Apr 18, 2004, in forum: Word VBA Beginners
    Replies:
    1
    Views:
    97
    Word Heretic
    Apr 20, 2004
  7. john smith

    vba ms word - read line from table cells

    john smith, Aug 22, 2006, in forum: Word VBA Beginners
    Replies:
    9
    Views:
    681
    Doug Robbins - Word MVP
    Aug 23, 2006
  8. StargateFanFromWork

    Deleting text - how to do, and prompting user before deleting?

    StargateFanFromWork, Apr 5, 2007, in forum: Word VBA Beginners
    Replies:
    13
    Views:
    220
Loading...