问题描述
此VBA代码旨在查找并用一个变量替换多个子字符串(在下面的示例中,当前只有一个名为FirstName
的变量)。
可以在装有Office 2016的PC上使用,但在装有Office 2019的另一台PC上则无法执行任何操作。在调试时,它会显示正确的替换信息,但保留子字符串"#First#"
而不替换它。
我为Windows和Excel运行了更新,没有任何改善。
Public Sub VariablesReplace()
'PURPOSE: Find & Replace a list of strings/values by variables
'SOURCE: https://www.thespreadsheetguru.com/the-code-vault/2014/4/14/find-and-replace-all?rq=fndList%20
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
Dim lastrowVariable1 As Long
Dim Row As Long
Dim FirstArrayNo As Long
Dim F1_Text As String
Dim F1_FirstName As String
Dim FirstName As Range
F1_Text = "G"
F1_FirstName = "T"
S_Presentation.NoUpdate
lastrowVariable1 = WhatsAppMsg.ListObjects("Table1").Range.Columns(4).Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevIoUs).Row
set FirstName = WhatsAppMsg.Range(F1_FirstName & "4:" & F1_FirstName & lastrowVariable1).Value
fndList = Array("#First#")
rplcList = Array(FirstName)
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
For Row = 4 To lastrowVariable1 +1
FirstArrayNo = Row - 3
WhatsAppMsg.Cells(Row,F1_Text).Replace What:=fndList(x),Replacement:=rplcList(x)(FirstArrayNo,1),_
LookAt:=xlPart,MatchCase:=False,_
SearchFormat:=False,ReplaceFormat:=False
Next
Next
S_Presentation.YesUpdate
End Sub
此代码在两台PC上均有效,但每次都限于一个变量(而不是具有替换多个变量的数组)
Public Sub VariablesReplace()
'PURPOSE: Find & Replace a list of strings/values by variables
'SOURCE: https://www.thespreadsheetguru.com/the-code-vault/2014/4/14/find-and-replace-all?rq=fndList%20
Dim lastrowVariable1 As Long
Dim Row As Long
Dim F1_Text As String
Dim F1_FirstName As String
F1_Text = "G"
F1_FirstName = "T"
S_Presentation.NoUpdate
lastrowVariable1=WhatsAppMsg.ListObjects("Table1").Range.Columns(4).Cells.Find("*",SearchDirection:=xlPrevIoUs).Row
For Row = 4 To lastrowVariable1 + 1
SendText1 = Replace(WhatsAppMsg.Range(F1_Text & Row).Value,"#First#",WhatsAppMsg.Range(F1_FirstName & Row).Value)
WhatsAppMsg.Range(F1_Text & Row).Value = SendText1
Next
S_Presentation.YesUpdate
End Sub
这是我开始的示例源:
Sub Multi_FindReplace()
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
fndList = Array("Canada","United States","Mexico")
rplcList = Array("CAN","USA","MEX")
'Loop through each item in Array lists
For x = LBound(fndList) To UBound(fndList)
'Loop through each worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
sht.Cells.Replace What:=fndList(x),Replacement:=rplcList(x),_
LookAt:=xlPart,_
SearchFormat:=False,ReplaceFormat:=False
Next sht
Next x
End Sub
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)