假设我有一个数组= {A,B,Y,X}。现在我有一张Excel工作表,它可以有动态的列数和Rows.Say如下例所示:
的输入 的
ColA ColB ColC ....
T P Y ....
C Y D ....
B A M ....
Z R X ....
的输出: 的
ColA ColB ColC ....
- - Y ....
- Y - ....
B A - ....
- - X ....
如果所有列都只有Array值,如果找到任何其他值,则需要将它们替换为 “ - ”
除了相对慢的循环技术外,使用VBscript还有更快的过程吗?
谢谢,
答案 0 :(得分:1)
Sub Macro1()
Dim arr, i, rng As Range
arr = Array("X", "Y", "Z")
Set rng = ActiveSheet.Range("A1").CurrentRegion
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
For i = LBound(arr) To UBound(arr)
rng.Replace What:=arr(i), Replacement:="-", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Next i
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
修改强>
Sub KeepValues()
Dim arr, arrVals, i, rng As Range, r, c
Dim keepval As Boolean
arr = Array("X", "Y", "Z")
Set rng = ActiveSheet.Range("A1").CurrentRegion
arrVals = rng.Value
For r = 1 To UBound(arrVals, 1)
For c = 1 To UBound(arrVals, 2)
keepval = False
For i = LBound(arr) To UBound(arr)
If arr(i) = arrVals(r, c) Then
keepval = True
Exit For
End If
Next i
If Not keepval Then arrVals(r, c) = ""
Next c
Next r
rng.Value = arrVals
End Sub