问题
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
You can see the attached Pic for more clearification
回答1:
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:
** EDIT: 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
来源:https://stackoverflow.com/questions/53820982/how-can-i-keep-the-selected-tables-cells-selected-after-i-run-the-macro