Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-03-2023, 09:38 AM
cdl cdl is offline VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after Windows 7 64bit VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after Office 2010 64bit
Novice
VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after
 
Join Date: Aug 2014
Posts: 2
cdl is on a distinguished road
Default VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after

Hello,
the following code in VBA should move a selected text, from one cell to the adjacent cell to its right, after selecting the ENTER key.
I tried it, but it doesn't work. Am I wrong or missing something?



I don't have to move the contents of a cell, just what is selected in a cell.
Example:
in a table cell, I have this sentence: Check inserted files.
I would like that, by selecting inserted files and pressing the ENTER key, the two selected words were moved to the adjacent cell on the right.
To run the code, I insert this into the VB page with the Develop menu (ALT+F11).

Here is the code:
Sub MoveTextInTable()
Dim oTable As Table
Dim oCell as cell
Dim oSelection as a selection
Set oSelection = Application.Selection

If oSelection.Information(wdWithInTable) = True then
Set oTable = oSelection.Tables(1)
Set oCell = oSelection.Cells(1)
If not oCell.Next is nothing then ' Check if next cell exists
oCell.Range.Cut
oCell.Next.Range.InsertBefore Selection.Text
Else
MsgBox "Cannot move text to the right, as there is no adjacent cell."
End If
Else
MsgBox "Select a cell within a table."
End If
End sub

Carlo - Italy
Reply With Quote
  #2  
Old 02-05-2023, 10:34 PM
gmayor's Avatar
gmayor gmayor is offline VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after Windows 10 VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after How can I select all the text in a table cell when it contains multiple paragraphs? wrdy Word 4 05-18-2022 12:25 AM
VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after find and replace a number that's always in a table and always adjacent to a cell with known text mbcohn Word VBA 35 04-13-2021 11:40 AM
extract time from a cell and move it to front of cell before text ewso Excel 20 03-19-2017 11:34 AM
VBA-Table Word 2019 - Select and move a text from one cell to the adjacent cell to its right, after VBA code to select one word from a text in a cell and place that word in next cell Shinaj Excel 2 05-01-2014 01:50 PM
Auto-populate an MS Word table cell with text from a diff cell? dreamrthts Word Tables 0 03-20-2009 01:49 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:49 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft