Word VBA 代码用于选择单元格中的文本,将特殊剪切并粘贴回同一单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18465737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:00:04  来源:igfitidea点击:

Word VBA Code to select text in a cell, cut and paste special back into the same cell

vbaword-vba

提问by AneeshS

I have a table with two columns and "x" number of rows.

我有一个包含两列和“x”行数的表格。

In the 2nd column is formatted text which I would like to change to unformatted text.

在第二列中是格式化文本,我想将其更改为无格式文本。

The manual way of doing so is:

这样做的手动方法是:

Select the whole cell in the 2nd column » Cut » Click Edit » Click Paste Special » Click Unformatted

选择第二列中的整个单元格»剪切»单击编辑»单击选择性粘贴»单击未格式化

The idea is to paste the unformatted text back into the cell it was Cut from and then move down to the cell below.

这个想法是将未格式化的文本粘贴回它从中剪切的单元格,然后向下移动到下面的单元格。

I would really appreciate some code that can apply this to all the cells in the 2nd column of a table.

我真的很感激一些可以将其应用于表格第二列中的所有单元格的代码。

采纳答案by AneeshS

Here is the solution to my problem. A friend had a piece of code which I manipulated to suit my needs:

这是我的问题的解决方案。一位朋友有一段代码,我对其进行了操作以满足我的需要:

Sub CutAndPasteSpecialUnformatted()

        Dim value As Variable

        ' Process every row in the current table. '
        Dim row As Integer
        Dim rng As Range

        For row = 1 To Selection.Tables(1).Rows.Count
            ' Get the range for the rightmost cell. '
            Selection.Collapse Direction:=wdCollapseStart
            Set rng = Selection.Tables(1).Cell(row, Column:=2).Range

            ' For each, toggle text in rightmost cell. '
            rng.Select
            Selection.Copy
            Selection.Delete
            rng.Select
            Selection.Style = ActiveDocument.Styles("Normal")
            Selection.Delete
            Selection.Collapse Direction:=wdCollapseStart
            Selection.Range.PasteSpecial DataType:=wdPasteText
        Next

End Sub