用序列号替换逗号字符
Replacing Comma character with Serial No
代替
逗号 space 阿尼尔、逗号 space 苏尼尔等
如下图:
, Anil, Sunil etc
。
我想在同一行中提供序列号,例如:
(1) Anil (2) Sunil etc
'程序不得不做很多不必要的工作。有没有更好的方法呢
'将 Curser 放在第一个逗号之前的任何位置
Sub GiveSerialToLinerPoints()
x = ActiveDocument.Range(0, Selection.Paragraphs(1). _
Range.End).Paragraphs.Count
i = 0
For Each char In ActiveDocument.Paragraphs(x).Range.Characters
If char = "," Then
i = i + 1
End If
Next char
TotalCommas = i
For i = 1 To TotalCommas
With Selection
.StartIsActive = False
.Extend Character:=","
.Collapse Direction:=wdCollapseEnd
.MoveLeft
.Expand Unit:=wdCharacter
If .Text = "," Then
.Text = " (" & i & ")"
End If
End With
Next i
End Sub
对于输出,从文档中捕获字符串后,您可以使用 Split()
以逗号作为分隔符拆分字符串,然后通过循环构建新的结果字符串。
类似于:
Sub foo()
Dim StartString As String
Dim ResultString As String
Dim TempSplit As Variant
Dim LoopCounter As Long
StartString = "Anil, Sunil"
TempSplit = Split(MyString, ",")
For LoopCounter = 1 To UBound(TempSplit) + 1
ResultString = ResultString & TempSplit(LoopCounter - 1) & "(" & LoopCounter & ")"
Next LoopCounter
Debug.Print ResultString
End Sub
以字符串开头:
"Anil, Sunil"
并打印到 immidiate window:
(1)Anil (2)Sunil
另一种方法是倒数 Characters
:
Sub GiveSerialToLinerPoints()
Dim i As Long, rng As Range, n As Long
Set rng = Selection.Paragraphs(1).Range
n = 1 + Len(rng.Text) - Len(Replace(rng.Text, ",", "")) '# of commas plus one
For i = rng.Characters.Count To 1 Step -1
If rng.Characters(i).Text = "," Then
rng.Characters(i).Text = " (" & n & ")"
n = n - 1
End If
Next i
rng.Characters(1) = "(1) " & rng.Characters(1).Text
End Sub
尝试:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ","
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While .Find.Execute
i = i + 1
.Text = "(" & i & ")"
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " commas replaced."
End Sub
要将 F/R 限制为插入点所在的段落,您可以使用:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
With Selection.Paragraphs.First
Set Rng = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ","
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While .Find.Execute
If .InRange(Rng) Then
i = i + 1
.Text = "(" & i & ")"
Else
Exit Do
End If
.Collapse wdCollapseEnd
Loop
End With
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
'站点:我在 Whosebug 中的帐户
'作者:塞缪尔·埃弗森
' 代替逗号 space Anil Sharaf 逗号 space Sunil Sharaf 喜欢
'、Anil Sharaf、Sunil Sharaf 等 如何在同一行中输入序列号,例如:
'(1) 阿尼尔·沙拉夫 (2) 苏尼尔·沙拉夫等
'Select 逗号分隔的单词。
'然后 运行 这个宏按 F5
Sub GiveSerialToCommaDelimitedWordsSelectMethod_Quick()
Dim StartString As String
Dim ResultString As String
Dim TempSplit As Variant
Dim LoopCounter As Long
If Selection.Type = wdSelectionIP Then
MsgBox "You need to select the Comma Delimited Words."
Exit Sub
End If
StartString = Selection.Text
TempSplit = Split(StartString, ",")
For LoopCounter = 1 To UBound(TempSplit)
ResultString = ResultString & "(" & LoopCounter & ")" & TempSplit(LoopCounter) & " "
Next LoopCounter
Selection.Text = ResultString
End Sub
代替
逗号 space 阿尼尔、逗号 space 苏尼尔等
如下图:
, Anil, Sunil etc
。 我想在同一行中提供序列号,例如:
(1) Anil (2) Sunil etc
'程序不得不做很多不必要的工作。有没有更好的方法呢
'将 Curser 放在第一个逗号之前的任何位置
Sub GiveSerialToLinerPoints()
x = ActiveDocument.Range(0, Selection.Paragraphs(1). _
Range.End).Paragraphs.Count
i = 0
For Each char In ActiveDocument.Paragraphs(x).Range.Characters
If char = "," Then
i = i + 1
End If
Next char
TotalCommas = i
For i = 1 To TotalCommas
With Selection
.StartIsActive = False
.Extend Character:=","
.Collapse Direction:=wdCollapseEnd
.MoveLeft
.Expand Unit:=wdCharacter
If .Text = "," Then
.Text = " (" & i & ")"
End If
End With
Next i
End Sub
对于输出,从文档中捕获字符串后,您可以使用 Split()
以逗号作为分隔符拆分字符串,然后通过循环构建新的结果字符串。
类似于:
Sub foo()
Dim StartString As String
Dim ResultString As String
Dim TempSplit As Variant
Dim LoopCounter As Long
StartString = "Anil, Sunil"
TempSplit = Split(MyString, ",")
For LoopCounter = 1 To UBound(TempSplit) + 1
ResultString = ResultString & TempSplit(LoopCounter - 1) & "(" & LoopCounter & ")"
Next LoopCounter
Debug.Print ResultString
End Sub
以字符串开头:
"Anil, Sunil"
并打印到 immidiate window:
(1)Anil (2)Sunil
另一种方法是倒数 Characters
:
Sub GiveSerialToLinerPoints()
Dim i As Long, rng As Range, n As Long
Set rng = Selection.Paragraphs(1).Range
n = 1 + Len(rng.Text) - Len(Replace(rng.Text, ",", "")) '# of commas plus one
For i = rng.Characters.Count To 1 Step -1
If rng.Characters(i).Text = "," Then
rng.Characters(i).Text = " (" & n & ")"
n = n - 1
End If
Next i
rng.Characters(1) = "(1) " & rng.Characters(1).Text
End Sub
尝试:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ","
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While .Find.Execute
i = i + 1
.Text = "(" & i & ")"
.Collapse wdCollapseEnd
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " commas replaced."
End Sub
要将 F/R 限制为插入点所在的段落,您可以使用:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
With Selection.Paragraphs.First
Set Rng = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ","
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While .Find.Execute
If .InRange(Rng) Then
i = i + 1
.Text = "(" & i & ")"
Else
Exit Do
End If
.Collapse wdCollapseEnd
Loop
End With
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub
'站点:我在 Whosebug 中的帐户 '作者:塞缪尔·埃弗森
' 代替逗号 space Anil Sharaf 逗号 space Sunil Sharaf 喜欢 '、Anil Sharaf、Sunil Sharaf 等 如何在同一行中输入序列号,例如: '(1) 阿尼尔·沙拉夫 (2) 苏尼尔·沙拉夫等
'Select 逗号分隔的单词。 '然后 运行 这个宏按 F5
Sub GiveSerialToCommaDelimitedWordsSelectMethod_Quick()
Dim StartString As String
Dim ResultString As String
Dim TempSplit As Variant
Dim LoopCounter As Long
If Selection.Type = wdSelectionIP Then
MsgBox "You need to select the Comma Delimited Words."
Exit Sub
End If
StartString = Selection.Text
TempSplit = Split(StartString, ",")
For LoopCounter = 1 To UBound(TempSplit)
ResultString = ResultString & "(" & LoopCounter & ")" & TempSplit(LoopCounter) & " "
Next LoopCounter
Selection.Text = ResultString
End Sub