【问题标题】:Replacing Comma character with Serial No用序列号替换逗号字符
【发布时间】:2021-06-03 23:47:20
【问题描述】:

代替
逗号空格 Anil、逗号空格 Sunil 等 如下图:

、阿尼尔、苏尼尔等

。 我想在同一行中提供序列号,例如:

(1) 阿尼尔 (2) 苏尼尔等

'程序要做很多不必要的工作。有没有更好的方法。

'将光标放在第一个逗号之前的任何位置

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

【问题讨论】:

    标签: vba ms-word str-replace


    【解决方案1】:

    试试:

    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
    

    【讨论】:

    • 对不起!我忘了告诉我只希望在插入点所在的段落中执行此操作。这会更改文档中的所有逗号。我尝试给段落编号变量,但它什么也没做。喜欢:“使用 Active document.paragraphs(x).range”谢谢。
    • 你真的应该在你的问题中包含这些重要的细节。查看更新的答案。
    • 我还知道了如何将 F/R 限制在一个段落中。
    【解决方案2】:

    对于输出,一旦从文档中捕获字符串,您可以使用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
    

    以字符串开头:

    “阿尼尔,苏尼尔”

    并打印到 immidiate 窗口:

    (1)阿尼尔 (2)苏尼尔

    【讨论】:

      【解决方案3】:

      另一种方法是倒数 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
      

      【讨论】:

      • 谢谢蒂姆,这也有效。此外,我还学会了如何阅读段落中的每个字符或任何范围对象。
      【解决方案4】:

      '站点:我在 StackOverflow 中的帐户 '由萨缪尔·埃弗森(Samual Everson)

      ' 代替逗号空格 Anil Sharaf 逗号空格 Sunil Sharaf 赞 '、Anil Sharaf、Sunil Sharaf 等 如何在同一行中提供序列号,如: '(1) Anil Sharaf (2) Sunil Sharaf 等

      '选择逗号分隔的单词。 '然后按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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多