View Single Post
 
Old 02-05-2023, 10:34 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You appear to have made up some of your own syntax and in any case it would not work when you press the enter key. It is not clear whether you want some or all of the text moved. The following will facilitate both. Note the Cell.Next command will move to the next row if the selected cell is the last in the row, so the code checks to see if the cell is the last in the row, so that Next only works in the same row.

Code:
Sub MoveSomeTextInCell()
'Moves selected text within a cell
Dim oTable As Table
Dim oCell As Cell
Dim oRng As Range
    If Selection.Information(wdWithInTable) = True Then
        If Len(Selection.Range) = 0 Then
            MsgBox "Nothing selected", vbCritical
            Exit Sub
        End If
        Set oRng = Selection.Range
        Set oTable = oRng.Tables(1)
        Set oCell = oRng.Cells(1)
        If oCell.RowIndex = oCell.Next.RowIndex Then    ' Check if next cell exists
            oCell.Next.Range.InsertBefore oRng.Text
            oRng.Text = ""
        Else
            MsgBox "Cannot move text to the right, as there is no adjacent cell.", vbCritical
        End If
    Else
        MsgBox "Select a cell within a table.", vbCritical
    End If
End Sub


Sub MoveAllTextInCell()
'moves all of selected cell contents
Dim oTable As Table
Dim oCell As Cell
Dim oRng As Range
    If Selection.Information(wdWithInTable) = True Then
        Set oTable = Selection.Tables(1)
        Set oCell = Selection.Cells(1)
        Set oRng = oCell.Range
        oRng.End = oRng.End - 1
        If oCell.RowIndex = oCell.Next.RowIndex Then
            oCell.Next.Range.InsertBefore oRng.Text
            oRng.Text = ""
        Else
            MsgBox "Cannot move text to the right, as there is no adjacent cell.", vbCritical
        End If
    Else
        MsgBox "Select a cell within a table.", vbCritical
    End If
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote