【问题标题】:Dynamically replace text in vba动态替换vba中的文本
【发布时间】:2012-05-21 23:08:12
【问题描述】:

我正在尝试创建一个例程来替换给定字符串中可变数量的值。例如,如果我在电子表格中有一个基本文本:

“您选择的车辆 {0} 表明您的轮胎应介于 {1} 和 {2} 之间。但是,您已为此车辆输入了 {3} 个轮胎。请相应地更新记录。”

我想用来自应用程序的常量值或用户输入的其他变量替换标记。我正在尝试创建一个带有如下签名的例程,其中 RowID 是电子表格中基本文本所在的行,ReplacementValues 是一个包含 n 个变量的数组:

Sub ShowMsg(ByVal RowID As Integer, Optional ByVal ReplacementValues As Variant)

我无法弄清楚如何在每次迭代中不重复基本消息的整个文本的情况下循环遍历文本并替换每个标记。如果可能的话,我希望保持例程相当通用,而不是特定于 Excel,以防我以后需要将应用程序移动到数据库中。

希望我已经充分解释了这一点;任何帮助将不胜感激。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    你可以;

    Sub ShowMsg(ByVal RowID As Integer, Optional ReplacementValues As Variant)
    Dim data As String, i As Long
    If Not IsMissing(ReplacementValues) Then
        data = Range("A" & RowID).Value
        For i = 0 To UBound(ReplacementValues)
            data = Replace(data, "{" & i & "}", ReplacementValues(i))
        Next
        msgbox data
    End If
    End Sub
    

    调用;

    Dim a() As Variant: a = Array("aa", "bb", "cc")
    ShowMsg 8, a
    

    或替代方案:

    Sub ShowMsg(ByVal RowID As Integer, ParamArray ReplacementValues() As Variant)
    Dim data As String, i As Long
    If Not IsMissing(ReplacementValues) Then
        data = Range("A" & RowID).Value
        If IsArray(ReplacementValues(0)) Then ReplacementValues = ReplacementValues(0)
        For i = 0 To UBound(ReplacementValues)
            data = Replace(data, "{" & i & "}", ReplacementValues(i))
        Next
        msgbox data
    End If
    End Sub
    

    可以以相同的方式调用,也可以附加序数参数;

    ShowMsg 8, "aa", "bb", "cc"
    

    【讨论】:

      【解决方案2】:

      首先将您的基本字符串更改为这样的内容

      BaseString = "Your vehicle selection of {VEHSEL} indicates you should have " & _
                   "between {nTYRE1} and {nTYRE2} tires. However, you have entered " & _
                   "{nTotTYRE} tires for this vehicle. Please update the record " & _
                   "accordingly."
      

      如果您现在注意到您有特定的关键字,例如

      VEHSEL - Vehicle Selection
      nTYRE1 - Lowest selection of tires
      nTYRE2 - Highest selection of tires
      nTotTYRE - Total tires selected
      

      从电子表格中获取值后,只需使用REPLACE 将上述关键字替换为相关值

      所以你的代码看起来像

      Option Explicit
      
      Sub Sample()
          Dim lVSell As Long, lT1  As Long, lT2  As Long, ltotT As Long
          Dim lRowID As Long
      
          lRowID = 5
      
          With Sheets("Sheet1")
              lVSell = .Range("A" & lRowID).Value
              lT1 = .Range("B" & lRowID).Value
              lT2 = .Range("C" & lRowID).Value
              ltotT = .Range("D" & lRowID).Value
      
              Debug.Print ShowMsg(lRowID, lVSell, lT1, lT2, ltotT)
          End With
      End Sub
      
      Function ShowMsg(ByVal RowID As Integer, ByVal VSel As Long, _
      ByVal T1 As Long, ByVal T2 As Long, ByVal totT As Long) As String
          Dim BaseString As String
      
          BaseString = "Your vehicle selection of {VEHSEL} indicates you should have " & _
                       "between {nTYRE1} and {nTYRE2} tires. However, you have entered " & _
                       "{nTotTYRE} tires for this vehicle. Please update the record " & _
                       "accordingly."
      
          BaseString = Replace(BaseString, "VEHSEL", VSel)
          BaseString = Replace(BaseString, "nTYRE1", T1)
          BaseString = Replace(BaseString, "nTYRE2", T2)
          BaseString = Replace(BaseString, "nTotTYRE", totT)
      
          ShowMsg = BaseString
      End Function
      

      我假设这些值存储在工作表 1 中,范围为 A5 到 D5。

      编辑

      快照

      高温

      【讨论】:

        【解决方案3】:

        你可以试试这个

        Sub test()
            Dim x As Variant
            x = Split("<String0>,<String1>,<String2>", ",")
            ShowMsg 23, "A", x
        End Sub
        
        Sub ShowMsg(ByVal RowID As Integer, ColID As String, Optional ByVal ReplacementValues As Variant)
            Dim nText$
            nText = Cells(RowID, ColID)
            For pos = LBound(ReplacementValues) To UBound(ReplacementValues)
                Dim searchtext$
                searchtext = "{" & CStr(pos) & "}"
                nText = Replace(nText, searchtext, ReplacementValues(pos))
            Next pos
            MsgBox nText
        End Sub
        

        【讨论】:

          猜你喜欢
          • 2012-10-18
          • 1970-01-01
          • 2015-12-10
          • 2013-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-10
          • 1970-01-01
          相关资源
          最近更新 更多