Consulting

Results 1 to 3 of 3

Thread: Merge Cell if Empty with Cell to the left

  1. #1
    VBAX Newbie
    Joined
    Nov 2012
    Posts
    2
    Location

    Merge Cell if Empty with Cell to the left

    Hi all,

    I have a two column table. I would like to find cells in column 2 that are empty. If they are empty then they should merge with column 1 in the same row. I would also like to loop through the table to check other rows.

    This works with the top row:
    If ActiveDocument.Tables.Count >= 1 Then
    With ActiveDocument.Tables(1)
    .Cell(Row:=1, Column:=1).Merge _
    MergeTo:=.Cell(Row:=1, Column:=2)
    .Borders.Enable = True
    End With

    Can anyone help me loop this so that it checks the whole table.?

    Many thanks and regards

  2. #2
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,433
    Location
    Try:
    [VBA]Sub Demo()
    Dim TblRw As Row
    If ActiveDocument.Tables.Count = 0 Then Exit Sub
    With ActiveDocument.Tables(1)
    For Each TblRw In .Rows
    With TblRw
    If .Cells.Count > 1 Then
    If Len(.Cells(2).Range.Text) = 2 Then
    .Cells(1).Merge MergeTo:=.Cells(2)
    End If
    End If
    End With
    Next
    .Borders.Enable = True
    End With
    End Sub[/VBA]
    PS: When posting code, please use the VBA button and post your code between the VBA tags.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Nov 2012
    Posts
    2
    Location

    Thumbs up

    Many thanks - your code has done the trick !


    Quote Originally Posted by macropod
    Try:
    [vba]Sub Demo()
    Dim TblRw As Row
    If ActiveDocument.Tables.Count = 0 Then Exit Sub
    With ActiveDocument.Tables(1)
    For Each TblRw In .Rows
    With TblRw
    If .Cells.Count > 1 Then
    If Len(.Cells(2).Range.Text) = 2 Then
    .Cells(1).Merge MergeTo:=.Cells(2)
    End If
    End If
    End With
    Next
    .Borders.Enable = True
    End With
    End Sub[/vba]
    PS: When posting code, please use the VBA button and post your code between the VBA tags.

Posting Permissions

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