我如何改进下面的代码来替换基于名为“Table1”的表(在表“DataMatch”中)的单元格,但仅在名为“data”的特定表单中替换单元格A1:B10。不要为其他床单做任何事情。以下代码无效。
Sub replacecells1()
Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant
'Create variable to point to your table
Set tbl = Worksheets("DataMatch").ListObjects("Table1")
'Create an Array out of the Table's Data
Set TempArray = tbl.DataBodyRange
myArray = Application.Transpose(TempArray)
'Designate Columns for Find/Replace data
fndList = 1
rplcList = 2
'Loop through each item in Array lists
For x = LBound(myArray, 1) To UBound(myArray, 2)
Sheets("Data").Range("A1:B10").Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next
End Sub
我的“数据”表是(必须发生替换的地方):
我的“MatchData”表(其中包含table1)是:
答案 0 :(得分:0)
您只需要使用以下代码替换myArray循环中的所有内容,以仅替换Range(A1:B10)
工作表中Data
中的信息
For x = LBound(myArray, 1) To UBound(myArray, 2)
Sheets("Data").Range("A1:B10").Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next