【问题标题】:Excel VBA Replace the nth word in a stringExcel VBA替换字符串中的第n个单词
【发布时间】:2021-05-01 16:31:57
【问题描述】:

我的问题如下:

我有两组字符串。 “单词”用“+”隔开。

字符串 1:A25+F47+w41+r21+h65

字符串 2:4+7+4+4+2

我有一个文本框,用于识别字符串 1 中的单词“w41”。它是字符串中的第三个单词。我想替换字符串 2 中的第三个单词,那就是第二个“4”

到目前为止我所拥有的是:

我正在使用 split 函数来拆分字符串 1,其中有一个“+”:

Result=Split(String1, "+")

然后我用UBound找到w41的位置,结果是3。

FindWordPosition = UBound(Result()) + 1

现在我想以同样的方式拆分字符串 2。但后来我想将字符串 2 中的第三个单词从“4”更改为“3”,然后再将其组合在一起。结果是:

字符串 2:4+7+3+4+2

但我不知道该怎么做:(

【问题讨论】:

    标签: excel vba string replace split


    【解决方案1】:

    您可以简单地循环数组并在找到时退出:

    Public Sub BuildPositions()
       
        Const Criteria  As String = "w41"
        Const String1   As String = "A25+F47+w41+r21+h65"
        Const String2   As String = "4+7+4+4+2"
        Const Delimiter As String = "+"
        
        Dim Results()   As String
        Dim Positions() As String
        Dim Index       As Integer
    
        Results = Split(String1, Delimiter)
        Positions = Split(String2, Delimiter)
        
        For Index = LBound(Results) To UBound(Results)
            If Results(Index) = Criteria Then
                Exit For
            End If
        Next
        If Index <= UBound(Results) Then
            ' Result was located.
            Positions(Index) = 1 + Index
        End If
        
        Debug.Print "Results:", String1
        Debug.Print "Positions1:", String2
        Debug.Print "Positions2:", Join(Positions, Delimiter)
        
    End Sub
    

    输出:

    Results:      A25+F47+w41+r21+h65
    Positions1:   4+7+4+4+2
    Positions2:   4+7+3+4+2
    

    【讨论】:

      【解决方案2】:

      一种方法是使用ArrayList

      不确定要用什么替换匹配的项目。在下面的代码中,它被替换为序列号,它与您描述的内容相匹配,但可能不是您真正想要的。

      Option Explicit
      Sub due()
          Const str1 As String = "A25+F47+w41+r21+h65"
          Const str2 As String = "4+7+4+4+2"
          Const strMatch As String = "w41"
          Dim AL As Object
          Dim v, w, I As Long
          
      Set AL = CreateObject("System.Collections.ArrayList")
      
      'put str2 into arrayList
      v = Split(str2, "+")
      For Each w In v
          AL.Add w
      Next w
      
      'Check str1 against matchStr to get positions and act on the item in AL at that position
      v = Split(str1, "+")
      For I = 0 To UBound(v)
      'Note that arrayList index and "Split" array are zero-based
          If strMatch = v(I) Then
              AL.removeat I 'remove item in the same position as the position of the matched item
              AL.Insert I, I + 1 'Insert new item at that same position. Could be anything. I chose I+1 to match what you wrote in your question.
          End If
      Next I
      
      Debug.Print Join(AL.toarray, "+")
          
      End Sub
      
      

      =&gt; 4+7+3+4+2

      【讨论】:

      • 谢谢你,太棒了!但是,如果我将常量更改为变量,为什么它不起作用。所以,我改变了你的代码: Const str1 As String = "A25+F47+w41+r21+h65" with Dim str1 as string str1=ActiveWorkbook.Worksheets("Sheet1").TextBox1 但这不会给出正确的结果跨度>
      • @Richard 应该可以。添加Debug.Print 行以检查变量的内容。
      • 我一定是写错了一些代码,因为我重新编写了它,现在它可以工作了!谢谢你的帮助,你让我开心! :) 我希望你的一天会很棒!
      • @Richard 很高兴为您提供帮助。由于我的回答满足了您的问题,如果您能将其标记为已接受,我将不胜感激。您可以阅读What should I do when someone answers my question 了解更多信息。
      【解决方案3】:

      用索引替换

      • 这个例子应该会让你站起来。

      守则

      Option Explicit
      
      ' Results
      ' For w41:
      ' A25+F47+w41+r21+h65
      ' 4+7+3+4+1
      ' For h65:
      ' A25+F47+w41+r21+h65
      ' 4+7+4+4+5
      
      Sub replaceWithIndex()
         
          Const Criteria As String = "w41"
          Const str1 As String = "A25+F47+w41+r21+h65"
          Const str2 As String = "4+7+4+4+1"
          Const Delimiter As String = "+"
          
          Dim Split1() As String: Split1 = Split(str1, Delimiter)
          Dim Split2() As String: Split2 = Split(str2, Delimiter)
          
          ' Note that an array obtained with 'Split' is always zero-based, while
          ' the result of 'Application.Match' is always one-based (hence '- 1').
          
          Dim cMatch As Variant: cMatch = Application.Match(Criteria, Split1, 0)
      
          If IsNumeric(cMatch) Then
              Split2(cMatch - 1) = cMatch
              Debug.Print "Source"
              Debug.Print str1
              Debug.Print str2
              Debug.Print "Result"
              Debug.Print Join(Split1, Delimiter)
              Debug.Print Join(Split2, Delimiter)
          End If
          
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 2019-08-08
        • 2018-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多