[英]Why is a small Excel VBA Macro is running extremely slow
I am writing a short macro to hide all customers that have no current sales for the current year. 我正在编写一个简短的宏来隐藏当前没有当前销售额的所有客户。 The YTD sales are in the K column (specifically K10-250).
年初至今销售额在K栏(特别是K10-250)。 Those cells use a vlookup to pull data from another tab where we dump data.
这些单元格使用vlookup从我们转储数据的另一个选项卡中提取数据。 My question is why on earth would this macro take 10-15minutes to run?
我的问题是为什么这个宏需要10-15分钟才能运行? I have a similar macro on another spreadsheet that takes only 2-3 minutes for over 1,500 rows.
我在另一个电子表格上有一个类似的宏,超过1,500行只需2-3分钟。 I have already turned off screen updating.
我已经关闭了屏幕更新。 I can't think of anything else that would speed it up.
我想不出任何会加速它的事情。
Sub HideNoSlackers()
'
' HideNoSlackers Macro
'
'
Application.ScreenUpdating = False
'
Sheets("CONSOLIDATED DATA").Select
Dim cell As Range
For Each cell In Range("K10:K250")
If cell.Value = 0 Then
cell.EntireRow.Hidden = True
Else
cell.EntireRow.Hidden = False
End If
Next
End Sub
You might want the calculation to be set Manual before hiding the rows? 您可能希望在隐藏行之前将计算设置为手动? Also you can get rid of
If
statements in your case. 你也可以在你的情况下摆脱
If
语句。 Try this: 尝试这个:
Sub HideNoSlackers()
Dim cell As Range, lCalcState As Long
Application.ScreenUpdating = False
' Record the original Calculation state and set it to Manual
lCalcState = Application.Calculation
Application.Calculation = xlCalculationManual
For Each cell In ThisWorkbook.Worksheets("CONSOLIDATED DATA").Range("K10:K250")
cell.EntireRow.Hidden = (cell.Value = 0)
Next
' Restore the original Calculation state
Application.Calculation = lCalcState
Application.ScreenUpdating = True ' Don't forget set ScreenUpdating back to True!
End Sub
Sub HideNoSlackers()
Dim cell As Range, rng As Range, rngHide As Range
Set rng = Sheets("CONSOLIDATED DATA").Range("K10:K250")
rng.EntireRow.Hidden = False
For Each cell In rng.Cells
If cell.Value = 0 Then
If Not rngHide Is Nothing Then
Set rngHide = Application.Union(rngHide, cell)
Else
Set rngHide = cell
End If
End If
Next
If Not rngHide Is Nothing Then rngHide.EntireRow.Hidden = True
End Sub
Why are you doing this with a macro? 你为什么用宏做这个?
If you create a table over the data, you can set up a filter on the sales column that will show only those where sales<> 0. 如果您在数据上创建表格,则可以在销售列上设置过滤器,该过滤器仅显示销售额<> 0的过滤器。
Macros are useful in excel but the majority of actions that people turn to macros for can be done natively in excel. 宏在Excel中非常有用,但人们转向宏的大多数操作都可以在excel中本地完成。
there must be something else that's wrong. 一定有别的东西是错的。 Try without .Selecting the sheet but that's not a huge improvement
尝试没有。选择表格,但这不是一个巨大的进步
Note rows are visible by default so the Else statement should be optional really. 注意行默认是可见的,因此Else语句应该是可选的。
Sub HideNoSlackers()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Sheets("CONSOLIDATED DATA").Cells.EntireRow.Hidden = False
Dim cell As Range
For Each cell In Sheets("CONSOLIDATED DATA").Range("K10:K250")
If cell.Value = 0 Then cell.EntireRow.Hidden = True
Next
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
the shortest code to achieve the same Goal in a very different way: 以最不同的方式实现相同目标的最短代码:
Sub column_K_not_NULL
Sheets("CONSOLIDATED DATA").Select
If ActiveSheet.FilterMode Then Selection.AutoFilter 'if an autofilter already exists this is removed
ActiveSheet.Range("$K$10:$K$250").AutoFilter Field:=1, Criteria1:="<>0"
End Sub
of course you could put in the standard minimums like 当然你可以加入标准的最小值
application.calculation = Manual
Application.ScreenUpdating = False
and other way round at the end. 最后的其他方式。 Max
马克斯
Try disabling page breaks. 尝试禁用分页符。 I had a similar problem that would happen after someone printed from the sheet.
我有一个类似的问题,在有人从工作表打印后会发生。 This turned on page breaks, and subsequent runs of the script would take forever.
这打开了分页符,后续运行的脚本将永远。
ActiveSheet.DisplayPageBreaks = False
We found out, that the program Syncplicity in the Version 4.1.0.1533 slows down macros up to 15times slower because events trigger syncplicity. 我们发现,版本4.1.0.1533中的程序Syncplicity减慢了宏速度的15倍,因为事件会触发syncplicity。
with 同
Application.EnableEvents = False Application.EnableEvents = False
;do your job here 在这里做你的工作
Application.EnableEvents = True Application.EnableEvents = True
the speed is back. 速度回来了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.