dynamicreplacevba中的文本
我正在尝试创build一个例程来replace给定string中的可变数量的值。 例如,如果我在电子表格中有一个基本文本:
“您的车辆select{0}表示您应该在{1}和{2}轮胎之间。 但是,您已经为此车辆input了{3}轮胎。 请相应地更新logging。“
我想用应用程序中的常量值或用户input的其他variablesreplace标记。 我正在尝试创build一个具有如下签名的例程,其中RowID是电子表格中基本文本所在的行,而ReplacementValues是n个variables的数组:
Sub ShowMsg(ByVal RowID As Integer, Optional ByVal ReplacementValues As Variant)
我无法弄清楚如何遍历文本并replace每个标记,而不必在每次迭代中重复基本消息的整个文本。 如果可能的话,我想保持相当通用的例程,而不是特定于Excel,以防需要稍后将应用程序移动到数据库。
希望我已经充分解释了这一点; 任何帮助,将不胜感激。
您可以;
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
或者另一种select:
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"
首先改变你的基础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."
如果你现在注意到你有特定的关键字
VEHSEL - Vehicle Selection nTYRE1 - Lowest selection of tires nTYRE2 - Highest selection of tires nTotTYRE - Total tires selected
一旦你从spreadhseet中获取了数据,只需使用REPLACE
将上述关键字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
我假设这些值存储在范围A5到D5的工作表1中。
编辑
快照
HTH
你可以试试这个
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