[英]Excel VBA - For Loop taking far too long to execute
I have a script which I have designed in order to hide rows that do not contain data, the script looks through column A starting from Row 7. If it finds rows that do not contain values, it will hide those rows from sight. 我有一个设计用来隐藏不包含数据的行的脚本,该脚本从第7行开始遍历A列。如果找到不包含值的行,它将隐藏这些行。 Unfortunately this script takes over 1 minute to run on large sheets in its present form. 不幸的是,此脚本要花费1分钟以上的时间才能在当前表单上运行在大型表单上。
Does anybody have suggestions on how to re-write this script in order to make it faster? 是否有人建议如何重新编写此脚本以使其更快? It needs to run in 5 seconds max 最多需要运行5秒
Sub hideAllRows()
Dim Checklist As Variant
UnlockSheet
Call Show_Hide("Row", "7:519", True)
Call Show_Hide("Row", "529:1268", True)
Checklist = ActiveSheet.Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For I = UBound(Checklist, 1) To LBound(Checklist, 1) Step -1
If Checklist(I, 1) <> "" Then
Rows(I & ":" & I).Select
Selection.EntireRow.Hidden = False
End If
Next I
I have edited your code in order to make things simpler. 我已经编辑了您的代码,以使事情变得更简单。 One of the issues is that your code is firing events "like crazy" (each time you do a Select, an event is fired). 问题之一是您的代码会引发事件“疯狂”(每次执行Select都会触发事件)。
A. If you want to use your code as is, I suggest you add at the beginning 答:如果您想按原样使用代码,建议您在开始时添加
Application.EnableEvents = False
and add in the last line: 并添加最后一行:
Application.EnableEvents = true
B. I suggest that you do the hiding "in one blow", after the loop has ended. B.我建议您在循环结束后“一击”隐藏。 Here is how: 方法如下:
Dim Checklist As Variant
dim sRowsToHide as string
UnlockSheet
Application.ScreenUpdating = False
Call Show_Hide("Row", "7:519", True)
Call Show_Hide("Row", "529:1268", True)
Checklist = ActiveSheet.Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For I = UBound(Checklist, 1) To LBound(Checklist, 1) Step -1
If Checklist(I, 1) <> "" Then
if sRowsToHide = "" then
sRowsToHide = I & ":" & I
else
sRowsToHide = sRowsToHide & "," & I & ":" & I
end if
End If
Next I
ActiveSheet.Range(sRowsToHide).EntireRow.Hidden = True
Application.ScreenUpdating = True
You can use the following line to see how such a thing would work: 您可以使用以下行来查看这种情况如何工作:
ActiveSheet.Range("2:2,14:14,17:17,19:19").EntireRow.Hidden = True
You can try using ScreenUpdating, it will only update the sheet once the loop is done instead of updating every time 您可以尝试使用ScreenUpdating,它只会在循环完成后更新工作表,而不是每次都更新
Dim Checklist As Variant
UnlockSheet
Application.ScreenUpdating = False
Call Show_Hide("Row", "7:519", True)
Call Show_Hide("Row", "529:1268", True)
Checklist = ActiveSheet.Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For I = UBound(Checklist, 1) To LBound(Checklist, 1) Step -1
If Checklist(I, 1) <> "" Then
Rows(I & ":" & I).Select
Selection.EntireRow.Hidden = False
End If
Next I
Application.ScreenUpdating = True
The following will hide all rows that have constants (eg typed values ) in column A. 下面将隐藏列A中所有具有常量(例如, 键入值 )的行。
Sub hide_A_values()
With ActiveSheet.Columns("A")
.SpecialCells(xlCellTypeConstants).EntireRow.Hidden = True
End With
End Sub
This next one will hide all rows that have formulas in column A. 下一个将隐藏A列中所有具有公式的行。
Sub hide_A_values()
With ActiveSheet.Columns("A")
.SpecialCells(xlCellTypeFormulas).EntireRow.Hidden = True
End With
End Sub
Finally, this will hide all rows that have constants (eg typed values ) or formulas in column A. 最后,这将隐藏A列中所有具有常量(例如, 键入的值 )或公式的行。
Sub hide_A_values()
With ActiveSheet.Columns("A")
Union(.SpecialCells(xlCellTypeConstants), .SpecialCells(xlCellTypeFormulas)).EntireRow.Hidden = True
End With
End Sub
The problem is that you have to provide error control or risk dealing with the dreaded Runtime error: 1004 No cells were found
if there are no constants or formulas to hide. 问题是您必须提供错误控制或冒着可怕的Runtime error: 1004 No cells were found
风险Runtime error: 1004 No cells were found
如果没有要隐藏的常量或公式, Runtime error: 1004 No cells were found
。 On Error Resume Next
typically takes care of this. On Error Resume Next
通常会解决此问题。
Sub hide_A_values()
With ActiveSheet.Columns("A")
On Error Resume Next
.SpecialCells(xlCellTypeConstants).EntireRow.Hidden = True
.SpecialCells(xlCellTypeFormulas).EntireRow.Hidden = True
On Error GoTo 0
End With
End Sub
The only case not covered by those is formulas returning empty strings (eg "") which are not considered truly blank. 唯一没有被这些情况覆盖的情况是公式返回的空字符串(例如““)被认为不是真正的空白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.