Excel 在vba中动态替换文本
我试图创建一个例程,替换给定字符串中数量可变的值。例如,如果我的电子表格中有一个基本文本: 您选择的车辆{0}表明您应该有{1}和{2}个轮胎。但是,您已经为此车辆输入了{3}个轮胎。请相应地更新记录 我想用应用程序中的常量值或用户输入的其他变量替换令牌。我正在尝试创建一个带有签名的例程,如下面所示,其中RowID是电子表格中基本文本所在的行,ReplacementValues是n个变量的数组:Excel 在vba中动态替换文本,excel,vba,Excel,Vba,我试图创建一个例程,替换给定字符串中数量可变的值。例如,如果我的电子表格中有一个基本文本: 您选择的车辆{0}表明您应该有{1}和{2}个轮胎。但是,您已经为此车辆输入了{3}个轮胎。请相应地更新记录 我想用应用程序中的常量值或用户输入的其他变量替换令牌。我正在尝试创建一个带有签名的例程,如下面所示,其中RowID是电子表格中基本文本所在的行,ReplacementValues是n个变量的数组: Sub ShowMsg(ByVal RowID As Integer, Optional ByVal
Sub ShowMsg(ByVal RowID As Integer, Optional ByVal ReplacementValues As Variant)
我不知道如何循环文本并替换每个标记,而不在每次迭代中重复基本消息的整个文本。如果可能的话,我希望保持例程相当通用,而不是特定于Excel,以防以后需要将应用程序移动到数据库中
希望我已经充分解释了这一点;任何帮助都将不胜感激。首先将您的基本字符串更改为以下内容
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
我假设值存储在表1中,范围为A5到D5
编辑
快照
你可以
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"
你可以试试这个
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
子测试()
Dim x作为变体
x=拆分(“,”,“,”)
ShowMsg 23,“A”,x
端接头
Sub ShowMsg(ByVal RowID为整数,ColID为字符串,可选ByVal ReplacementValues为变量)
模糊文本$
nText=单元格(RowID,ColID)
对于pos=LBound(ReplacementValues)到UBound(ReplacementValues)
暗搜索文本$
searchtext=“{”和CStr(pos)和“}”
nText=Replace(nText、searchtext、ReplacementValues(pos))
下一个位置
MsgBox-nText
端接头