excel - 用户表单执行时间过长
问题描述
我有这个用户窗体,查找需要很长时间。有什么办法可以减少那段时间吗?
这是用户窗体的文本框代码,我在其中放置了我要查找的内容:
Private Sub TXTBUSCAART_Change()
Application.ScreenUpdating = False
Sheets("CONCAT").Select
Range("A2").Select
LSTART.Clear
While ActiveCell.Value <> ""
M = InStr(1, ActiveCell.Value, UCase(TXTBUSCAART.Text))
If M > 0 Then
LSTART.ColumnCount = 9
LSTART.AddItem
LSTART.List(LSTART.ListCount - 1, 0) = ActiveCell.Value
ActiveCell.Offset(0, 2).Select
LSTART.List(LSTART.ListCount - 1, 1) = ActiveCell.Value
ActiveCell.Offset(0, -1).Select
LSTART.List(LSTART.ListCount - 1, 2) = ActiveCell.Value
ActiveCell.Offset(0, 2).Select
LSTART.List(LSTART.ListCount - 1, 3) = ActiveCell.Value
ActiveCell.Offset(0, 2).Select
LSTART.List(LSTART.ListCount - 1, 4) = ActiveCell.Value
ActiveCell.Offset(0, -1).Select
LSTART.List(LSTART.ListCount - 1, 5) = ActiveCell.Value
ActiveCell.Offset(0, 3).Select
LSTART.List(LSTART.ListCount - 1, 6) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
LSTART.List(LSTART.ListCount - 1, 7) = ActiveCell.Value
ActiveCell.Offset(0, -2).Select
LSTART.List(LSTART.ListCount - 1, 8) = ActiveCell.Value
ActiveCell.Offset(0, -6).Select
End If
ActiveCell.Offset(1, 0).Select
Wend
Sheets("REMITO").Select
Range("A1").Select
Application.ScreenUpdating = False
End Sub
解决方案
将数据放入数组并循环遍历它应该会快得多 - 像这样(我想我的列是正确的):
Private Sub TXTBUSCAART_Change()
Dim rowCount As Long, itemCount As Long, counter As Long, n As Long
Dim dataSheet As Worksheet
Dim dataIn, dataOut()
LSTART.Clear
LSTART.ColumnCount = 9
Set dataSheet = Sheets("CONCAT")
With dataSheet
rowCount = .Cells(.Rows.Count, "A").End(xlUp).Row
itemCount = Application.WorksheetFunction.CountIf(.Range("A2:A" & rowCount), "*" & TXTBUSCAART.Text & "*")
If itemCount > 0 Then
ReDim dataOut(1 To itemCount, 1 To 9)
dataIn = .Range("A2:I" & rowCount).Value
counter = 1
For n = 1 To UBound(dataIn)
M = InStr(1, dataIn(1, 1), UCase(TXTBUSCAART.Text))
If M > 0 Then
dataOut(counter, 1) = dataIn(n, 1)
dataOut(counter, 2) = dataIn(n, 3)
dataOut(counter, 3) = dataIn(n, 2)
dataOut(counter, 4) = dataIn(n, 4)
dataOut(counter, 5) = dataIn(n, 6)
dataOut(counter, 6) = dataIn(n, 5)
dataOut(counter, 7) = dataIn(n, 8)
dataOut(counter, 8) = dataIn(n, 9)
dataOut(counter, 9) = dataIn(n, 7)
counter = counter + 1
End If
Next
LSTART.List = dataOut
End If
End With
End Sub
推荐阅读
- angular - 带有图表的日历可以在组件中使用吗?
- asp.net-core-2.0 - 如何处理山魈模板中的语言支持?
- c++ - SIGABRT 和 SIGSEGV 有什么区别
- python - conda 在激活环境外寻找库
- android - 由于计算能力不同,使用TensorFlow-Lite时,模型在不同安卓设备上的运行时间可能会有很大差异
- selenium - 使用 Java 在 Selenium WebDriver 中查找分页表中的行数
- javascript - 无法创建正则表达式
- google-cloud-platform - 如何使用 python 为 IAM 中的用户授予 Bigquery 的个人权限
- python - 如何修复“无法加载原生 TensorFlow 运行时”。尝试在 gpu 上运行时出错?
- mysql - 为什么 MySQL 的 IS TRUE、IS NOT TRUE、IS FALSE、IS NOT FALSE 行为在 WHERE 和 HAVING 之间是不同的