1

I have a macro which I've been using to merge two cells together in a word table, but what I want to do is to get the cursor to move down by one cell, so that I can repeatedly press the shortcut key to repeat the command over and over.

The macro code that I have (shamelessy copied and pasted from a web page), is as follows:

Sub MergeWithCellToRight()
'
' MergeWithCellToRight Macro
'
'
Dim oRng As Range
Dim oCell As Cell
Set oCell = Selection.Cells(1)
If oCell.ColumnIndex = Selection.Rows(1).Cells.Count Then
MsgBox "There is no cell to the right?", vbCritical, "Error"
Exit Sub
End If
Set oRng = oCell.Range
oRng.MoveEnd wdCell, 1
oRng.Cells.Merge
Selection.Collapse wdCollapseStart
End Sub

I've attempted to add the following line just before the 'End Sub' statement

Selection.MoveDown wdCell, 1

but this generates the error, Run-time error '4120' Bad Parameter whenever I execute the macro.

Can anyone tell me how to correct this or what I'm doing wrong?

2 Answers 2

2

No idea if that could help, but I have the following:

Sub Merges2Cols()
Dim nbLines As Integer
    nbLines = 10     'you'd have to count the number of lines you want to merge
    For i = 1 To nbLines
        Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend
        Selection.Cells.Merge
        Selection.MoveDown Unit:=wdLine, Count:=1
    Next
End Sub
0

I don't know what you are doing wrong, but I just ran across the exact same problem. I found that this works for moving the cursor down one row.

Selection.MoveRight Unit:=wdCell, Count:=Selection.Tables(1).Columns.Count

And if you want to move up one row:

Selection.MoveLeft Unit:=wdCell, Count:=Selection.Tables(1).Columns.Count

This will work for tables where each row has the same number of columns. Not sure how it works if some rows have merged columns.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .