隐藏行的表单行为非常缓慢

由于某些原因,这个macros在macros观运行时performance的非常迟钝。 当每次尝试更改不属于范围的非隐藏单元的信息时,这就变得有问题,它仍然运行更新,并且需要将近5-10秒才能完成。

需要发生什么样的变化来缓解这个问题?

Private Sub Worksheet_Change(ByVal Target As Range) Dim c As Range For Each c In Range("A7:A98") If c.Value = 0 And c.Value = vbNullString Then c.EntireRow.Hidden = True End If Next c For Each c In Range("A7:A98") If c.Value <> 0 And c.Value <> vbNullString Then c.EntireRow.Hidden = False End If Next c End Sub 

你的逻辑看起来粗略,有点难以告诉你正在做什么,但你的逻辑可以缩短,并用来确定布尔.Hidden。

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("A7:A98")) Is Nothing Then On Error GoTo safe_exit Application.EnableEvents = False Dim trgt As Range For Each trgt In Intersect(Target, Range("A7:A98")) trgt.EntireRow.Hidden = CBool(trgt.Value = vbNullString) Next trgt End If safe_exit: Application.EnableEvents = True End Sub 

像这样的东西应该为你工作:

 Private Sub Worksheet_Change(ByVal Target As Range) Dim rCheck As Range Dim rCell As Range Dim rHide As Range Dim lCalc As XlCalculation Set rCheck = Me.Range("A7:A98") With Application lCalc = .Calculation .Calculation = xlCalculationManual .EnableEvents = False .ScreenUpdating = False End With On Error GoTo CleanExit If Not Intersect(Target, rCheck) Is Nothing Then rCheck.EntireRow.Hidden = False For Each rCell In rCheck If rCell.Value = 0 And rCell.Value = vbNullString Then If rHide Is Nothing Then Set rHide = rCell Else Set rHide = Union(rHide, rCell) End If End If Next rCell End If If Not rHide Is Nothing Then rHide.EntireRow.Hidden = True CleanExit: With Application .Calculation = lCalc .EnableEvents = True .ScreenUpdating = True End With End Sub