运行宏后,如何保留选定的表单元格? [英] How can i keep the selected tables cells, selected after i run the macro?

查看:103
本文介绍了运行宏后,如何保留选定的表单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i run the below mention macro to reverse the selected table cells words, the macro deselect the selected cells after its first run. I want the selected cells selected after this macro run so that i can call the second macro on the same selection.

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

您可以看到随附的图片,以进行进一步的澄清

推荐答案

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

Do you need to run the second macro separately? You could just add at the end of your macro to call the second one.

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

You probably want to pass the selection to the second macro something like this:

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

** added a bit more clarity (i hope) **

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

这篇关于运行宏后,如何保留选定的表单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆