1
votes

I have a table in word in which I merge 1 cell with text to the 2 empty cells left of it. Each time it merges it adds a new line under the text of the merged cell. This messes up the whole layout of the table. This table has been copied from Excel and pasted into Word. How do I fix this?

Dim table1 as Table
With table1
.Cell(Row:=1, Column:=3).Merge _ 
MergeTo:=.Cell(Row:=1, Column:=5)
End With
2
Mkay... Do you use some code to tag this as vba?? If yes, please add your code. If no, please change the tags and add more details. In both cases, it's edit time! ;)R3uK
BTW, I see that you didn't take the tour, nor accept any answer on your 7 questions, it's time to learn how SO works! Then go back to your profile, accept the answer that helped you on each question, then you can have some more help! ;)R3uK
Apologies, I've added in the code and addressed old questions.JoshD

2 Answers

1
votes

The Merge command can only merge two adjacent cells. You might use a loop to merge more cells.

Dim i As Long

With ActiveDocument.Tables(1).Rows(1)
    For i = 3 To 4
        .Cells(2).Merge .Cells(i)
    Next i
End With

Cells 3 and 4 are merged with cell(2).

1
votes

This variation includes two solutions for how to deal with existing content in cells being merged.

Private Sub TestMerge()
    Dim i As Long

    With ActiveDocument.Tables(1).Rows(1)
        For i = 3 To 4
'            .Cells(i).Range.Text = ""
            .Cells(2).Merge .Cells(i)
        Next i
        .Cells(2).Range.Text = ""
    End With
End Sub

The first solution deletes everything in cells 3 and 4 but retains content of cell(2). In the above code it is rendered mute by the apostrophe preceding the code.

The second way is to remove everything from the merged cells. I would recommend to run this version only while Application.ScreenUpdating = False because it will cause a lot of flicker.

There are several more way, but in order to recommend the one most suitable for your needs one would need to have a better understanding of what you want. BTW, if at all possible, remove the blanks from the table at the time they are entered. Having blanks floating around your document and popping up when they are least expected or wanted is never a good idea.