动态替换vba中的文本

时间:2012-05-21 14:32:36

标签: excel vba

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

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

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

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

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

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

3 个答案:

答案 0 :(得分:2)

你可以;

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"

答案 1 :(得分:1)

首先将您的基本字符串更改为此类

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

从spreadhseet中获取值后,只需使用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

我假设这些值存储在Sheet 1中,范围是A5到D5。

修改

<强>快照

enter image description here HTH

答案 2 :(得分:0)

你可以试试这个

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