Bonjour,
Dans son message, < Joanne > écrivait :
In this message, < Joanne > wrote:
|| I want to write a simple macro that will select the first
|| cell in a row of a table, merge it with the second cell,
|| then move down a row and do the same thing. Once it has
|| finished with the last row, it should stop. For the life
|| of me, I can't get this thing to work. Can anyone help?
|| Thank you in advance.
Just place the cursor anywhere in the table and run this macro:
(I was having fun with this so I added all kinds of checks and message
boxes.)
By adding another "For... / Next..." you could "easily" modify the code so
that it can act on all tables in the selection...
'_______________________________________
Dim StartRange As Range
Dim TwoCellRange As Range
Dim TableRange As Range
Dim i As Long
Dim OneCell As Boolean
OneCell = False
Set StartRange = Selection.Range
'In case selection spans more than one table, we
'will assume that user wants to act on first table
'in selection
With StartRange
If .Tables.Count > 0 Then
Set TableRange = .Tables(1).Range
Else
MsgBox "No table selected.", vbExclamation, _
"Action cancelled"
Exit Sub
End If
End With
With TableRange
For i = 1 To .Rows.Count
If .Rows(i).Cells.Count > 1 Then
Set TwoCellRange = .Rows(i).Cells(1).Range
TwoCellRange.End = .Rows(i).Cells(2).Range.End
TwoCellRange.Cells.Merge
Else
OneCell = True
End If
Next i
End With
If OneCell Then
MsgBox "Some (or all) the rows in the selected " _
& "table contain only 1 cell and were not " _
& "merged.", vbInformation, "One-Cell row(s)"""
End If
StartRange.Select
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org