运行宏后,如何保留选定的表格单元格?

时间:2018-12-17 18:19:59

标签: vba ms-word selection tablecell

当我运行下面提到的宏以反转选定的表格单元格单词时,该宏在其第一次运行后取消选择选定的单元格。我希望在此宏运行后选定的单元格处于选中状态,以便我可以在同一选择上调用第二个宏。

Private Sub CommandButton1_Click()

Dim rng As Word.Range
Dim cl As Word.Cell
Dim i As Integer, iRng As Word.Range
Dim oWords As Words
Dim oWord As Range

If Selection.Information(wdWithInTable) = True Then
    For Each cl In Selection.Cells
        Set rng = cl.Range
        rng.MoveEnd Word.WdUnits.wdCharacter, Count:=-1
        For i = 1 To rng.Words.Count
            Set iRng = rng.Words(i)
            'rng.Select

            Set oWord = iRng
            Do While oWord.Characters.Last.Text = " "
                Call oWord.MoveEnd(WdUnits.wdCharacter, -1)
            Loop
            Debug.Print "'" & oWord.Text & "'"
            oWord.Text = StrReverse(oWord.Text)

            Debug.Print Selection.Text

        Next i
    Next cl
End If

End Sub
Sub Align()

'Selection.RtlPara
 Selection.LtrPara

 End Sub

 Private Sub CommandButton2_Click()

 Call Align
 Call CommandButton1_Click
 Call Comma_Remove
 Call CommandButton1_Click

 End Sub

 Sub Comma_Remove()


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = ","
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

You can see the attached Pic for more clearification

1 个答案:

答案 0 :(得分:1)

是否需要单独运行第二个宏?您只需在宏的末尾添加即可调用第二个宏。

您可能希望将选择内容传递给第二个宏,如下所示:

**编辑:添加了更多的清晰度(我希望)**

Sub firstMacro(selection)

'' Do stuff with Selection
Debug.Print "This is the first macro and address of selection is: " & selection.Address

End Sub

Sub secondMacro(selection)

'' Do more stuff with Selection
Debug.Print "This is the second macro and address of selection is: " & selection.Address

End Sub


Private Sub CommandButton1_Click()

    Call firstMacro(selection)

    Call secondMacro(selection)

End Sub

Private Sub CommandButton2_Click(selection) '<--- THIS IS WRONG

End Sub