4

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

“您选择的车辆 {0} 表明您应该有 {1} 到 {2} 个轮胎。但是,您已为此车辆输入了 {3} 个轮胎。请相应地更新记录。”</p>

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

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

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

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

4

3 回答 3

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"
于 2012-05-21T14:55:47.197 回答
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

从电子表格中获取值后,只需使用相关值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。

编辑

快照

在此处输入图像描述 高温高压

于 2012-05-21T14:55:28.850 回答
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
于 2012-05-21T15:08:12.370 回答