【问题标题】:How can i keep the selected tables cells, selected after i run the macro?如何在运行宏后保留选定的表格单元格?
【发布时间】:2018-12-20 20:28:19
【问题描述】:

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

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

【问题讨论】:

    标签: vba ms-word selection tablecell


    【解决方案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
    

    【讨论】:

    • .duplicate 是你的朋友。
    • 我通过了选择,现在出现错误..!!
    • 在不知道错误是什么以及您的第二个宏的内容是什么的情况下,我无法再提供帮助。
    • 我添加了剩余的代码,现在你可以理解我想要实现的目标了。有关详细信息,前两个调用宏运行完美,但后两个仅适用于光标闪烁的唯一一个单元格。
    • 你仍然没有说你遇到了什么错误......如果你做了我建议的修改,我会帮助你纠正你做错的事情......当然,有更好的方法来实现你在那里做的事情,但我的建议是我能想到的最简单的解决方法。
    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 2015-10-17
    • 2019-09-23
    相关资源
    最近更新 更多