【发布时间】:2022-07-23 19:08:39
【问题描述】:
我正在尝试查找和替换列名。
我只想在第一行循环,但我的代码循环了整个工作表。
Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long
Dim data As Variant
Set sht = ActiveWorkbook.Worksheets("ERT")
fndList = Array("colon.", _
"Selec:", _
"Sele, _
"Submi")
rplcList = Array("A", "A1", "S1", "D1")
With sht
'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("Exp")
sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _
lookat:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
'Next sht
Next x
End With
我只想查找和替换从第一行 A1 到第一行末尾的数组值。
【问题讨论】:
-
VBA 帮助说明
WorkSheet.Cells属性:返回一个 Range 对象,该对象代表工作表上的所有单元格(不仅仅是当前正在使用的单元格。调用sht.Cells.Replace ...,其中sht是一个工作表。将sht改为引用Range。 -
好的,所以保留 sht 但声明一个用于替换这些列名的范围对象,例如
Dim rng As Range和rng = sht.Range("A1:F1")。然后拨打rng.Replace ...