[英]“Subscript out of range” error when looping through Array to Find/Replace in a column
I have two lists of names on an excel sheet (sh1 - Column A, Column D)
. 我在excel工作表上有两个名称列表(sh1 - Column A, Column D)
。 On another excel sheet (sh2 - Column B)
, I have a another list of names. 在另一个Excel工作表(sh2 - Column B)
,我还有另一个名称列表。 I am trying to find/replace Column A names on Column B with Column D. 我正在尝试用列D查找/替换列B上的列A名称。
My full code is listed at the bottom. 我的完整代码在底部列出。 I am getting a "Subscript out of range" error on this line: 我在此行收到“下标超出范围”错误:
Selection.Replace What:=fndArr(i), Replacement:=rplArr(i), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
I have checked the range and it seems like the code should loop through every cell in sh2 Column B looking for Column A data and replacing it with Column D data. 我已经检查了范围,似乎代码应该遍历sh2列B中的每个单元格以查找列A数据并将其替换为列D数据。
I'm drawing a total blank on this. 我为此画了一个空白。 Does anyone know why I am getting this error? 有人知道我为什么收到此错误吗? Thank you for your help. 谢谢您的帮助。
Column A | Column B | Column D
--------------------------------------------
Hugh Jackman | Hugh J | Hugh Jackman
Ronald Reagan | Ronald R | Ronald Reagan
John Adams | John A | John Adams
. 。
Sub CheckReplace()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim fndArr() As Variant
Dim rplArr() As Variant
Set sh1 = Sheets("CA")
Set sh2 = Sheets("FD")
''' turn off screen updating '''
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
''' Find/Replace CA '''
sh1.Activate
fndArr = sh1.Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row)
rplArr = sh1.Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
sh2.Activate
Columns("B").Select
For i = 0 To UBound(fndArr)
Selection.EntireColumn.Select
Selection.Replace What:=fndArr(i), Replacement:=rplArr(i), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Next i
''' turn on screen updating '''
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
EDIT: 编辑:
The following change in code allows it to run without throwing an error, but then it find/replaces the first values in Column A/B, eg Hugh Jackman Hugh J, but not Ronald Reagan, Ronald R: 下面的代码更改使它可以运行而不会引发错误,但是它会查找/替换A / B列中的第一个值,例如Hugh Jackman Hugh J,但不是Ronald Reagan,Ronald R:
fndArr = Array(sh1.Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row))
rplArr = Array(sh1.Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row))
When you equate an array to a range, you create a 1-based 2D array, even with a single column, so: 将数组等于范围时,即使只有一列,也会创建基于1的2D数组,因此:
For i = 1 To UBound(fndArr)
Selection.EntireColumn.Select
Selection.Replace What:=fndArr(I,1), Replacement:=rplArr(I,1), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Next I
EDIT: You also need to get rid of the selection stuff. 编辑:您还需要摆脱选择的东西。 Perhaps the following (not tested) 也许以下(未经测试)
'Delete these two lines
'sh2.Activate
'Columns("B").Select
With sh2.Columns("B")
For i = 0 To UBound(fndArr)
.Replace What:=fndArr(I,1), Replacement:=rplArr(I,1), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Next i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.