【发布时间】:2019-05-06 15:38:27
【问题描述】:
我正在编写一个宏,它将评估 O 到 V 列中的字段的 800 行左右。我读过阅读和写作花费的时间最长,这就是我本质上在做的事情。
因为有很多数据,所以运行速度很慢。运行所有内容可能需要一分钟,也许更多一点,它会说 Excel 在运行时没有响应。
我需要一些帮助来优化它,因为我对 VBA 不是很熟悉,但我已经尽我所能来加快速度。我读过使用二维数组会有所帮助,但我不知道在这种情况下如何工作。
任何帮助或建议将不胜感激!谢谢你的时间:-)
Sub Check_Missing()
Application.ScreenUpdating = False
Dim LastRow, LastRow2 As Long
Dim col
Dim i, j,
Dim M, N, P As String
Dim summarySh, resultsSh As Worksheet
Set summarySh = Sheets("summary")
Set resultsSh = Sheets("Results")
col = Array("O", "P", "Q", "R", "S", "T", "U", "V")
M = "Missing"
N = "No"
P = "Partial"
LastRow = summarySh.Range("A" & Rows.Count).End(xlUp).Row
LastRow2 = resultsSh.Range("A" & Rows.Count).End(xlUp).Row + 1
resultsSh.Range("A2:AC" & LastRow2).Clear
For i = 2 To LastRow
For j = LBound(col) To UBound(col)
If summarySh.Cells(i, col(j)).Value = M Or summarySh.Cells(i,
col(j)).Value = N Or summarySh.Cells(i, col(j)).Value = P Then
summarySh.Cells(i, col(j)).EntireRow.Copy
Destination:=resultsSh.Range("A" & Rows.Count).End(xlUp).Offset(1)
GoTo ContinueForLoop
End If
Next j
ContinueForLoop:
Next i
Application.ScreenUpdating = True
End Sub
这是一个附带问题,所以如果你碰巧知道,那就太好了,但如果不知道,我相信我能弄明白。
我必须比较两个工作簿(一个是我正在使用的工作簿,另一个是从外部下载的),我希望调用一个 Excel 加载项函数 Inquire,以便它会立即弹出,如果其他人将使用我的宏,因为它会更加用户友好。
【问题讨论】:
-
当您在同一行中声明变量时,您需要这样做:
Dim LastRow As Long, LastRow2 As Long,否则只有该行中的最后一个变量被声明为您想要的类型和其他变量类型为Variant。这可能会影响执行速度。