我已经使用宏大约 11 年了,没有任何问题。但是,在过去的 18 个月中,我一直在使用它的一些工作簿非常大,在这些情况下,宏可能需要很长时间才能运行。
为了解决这个问题,我修改了宏以禁用事件和屏幕更新,并使计算“手动”。这显着改善了事情,但有时改进还不够。
例如,昨天我运行宏将 288,000 个电子邮件地址列表与另一个 235,000 个电子邮件地址列表进行比较,以识别任何匹配项。这需要大约 14 小时才能完成(因此超出了典型的工作日)。
我做了一些研究(1、2等),并确定 usingMatch()
会比Find()
.
但是,我在修改要使用的代码时遇到问题Match()
。以下是使用的当前工作代码Find()
:
Sub FindMatchingData()
Application.EnableEvents = False
Application.Calculation = xlManual
Application.ScreenUpdating = False
Dim MySearchRange As Range
Dim c As Range
Dim findC As Variant
Set MyRange = Application.InputBox( _
Prompt:="Select the range of cells containing the data you are looking for:", Type:=8)
Set MySearchRange = Application.InputBox( _
Prompt:="Select the range you wish to investigate:", Type:=8)
Response = InputBox(Prompt:="Specify the comment you wish to appear to indicate the data was found:")
MyOutputColumn = Application.InputBox( _
Prompt:="Enter the alphabetical column letter(s) to specify the column you want the message to appear in.")
Set Sht = MyRange.Parent
For Each c In MyRange
If Not c Is Nothing Then
Set findC = MySearchRange.Find(c.Value, LookIn:=xlValues)
If Not findC Is Nothing Then
Sht.Range(MyOutputColumn & c.Row).Cells.Value = Response
End If
End If
Next
Excel.Application.SendKeys Keys:="^{HOME}", Wait:=True
DoEvents
Application.EnableEvents = True
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
MsgBox "Investigation completed."
End Sub
问题:
- 有人可以建议是否可以轻松修改上述代码以使用 Match() 代替,还是我最好完全从头开始?
- 如果可以对其进行修改,将不胜感激任何帮助,因为我似乎遇到了砖墙。
附加信息:宏的预期行为
此宏为用户提供了一种在同一工作表内或同一工作簿内跨工作表查找两个范围之间的匹配数据的简单方法。
预期的行为如下:
- 它提示用户:
- 选择包含他们正在寻找的数据的范围
- 选择要搜索的范围以查看数据是否也存在那里
- 输入用于指示任何匹配数据的注释
- 如果找到匹配项,请输入您希望将评论填充到的列
- Excel 然后继续运行所选范围之间的比较,并在找到匹配项的地方开始填充所选列。
- 完成后,它会向用户显示调查完成消息。
例如,如果用户在工作表 1 中选择范围 A2:A40000 作为包含要查找的数据,然后在工作表 2 中选择范围 C2:C2000 作为要搜索的范围,然后输入单词“是”作为后面的注释通过选择 D 作为列,完成后的预期结果是,如果找到数据,用户将看到工作表 1 中 D 列中的单元格将包含单词“是”。
正如您链接的文章和我在评论中所做的那样,循环数组比 Find 或 Match 更快,并使用 for 循环: