插入公式后的宏延迟问题 - Excel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了插入公式后的宏延迟问题 - Excel相关的知识,希望对你有一定的参考价值。
我在Excel
有一个中央数据库,它使用输入Column B
的特定值,并根据此值隐藏/取消隐藏其他相关的Columns
。这工作正常,直到我放入单独的公式,将不同的值复制到他们各自的sheets
。
例如。如果A
进入Column B
它应该Columns D:E
。然后一个单独的formula
将row
复制到一个单独的sheet
,其中包含所有的A's
。
问题是这个宏现在需要很长时间才能完成。我已禁用公式以使它们手动运行但它没有帮助。以下是隐藏/取消隐藏列的宏的示例。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim t As Range
For Each t In Intersect(Target, Range("B:B"))
Select Case (t.Value)
Case "A"
Columns("B:BQ").EntireColumn.Hidden = False
Columns("H:AD").EntireColumn.Hidden = True
Columns("AF:BL").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "B"
Columns("B:BQ").EntireColumn.Hidden = False
Columns("F:G").EntireColumn.Hidden = True
Columns("P:BP").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "C"
Columns("B:BQ").EntireColumn.Hidden = False
Columns("F:O").EntireColumn.Hidden = True
Columns("T:BL").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "D"
Columns("B:BP").EntireColumn.Hidden = False
Columns("E:S").EntireColumn.Hidden = True
Columns("AB:BL").EntireColumn.Hidden = True
Columns("BN:BP").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "E"
Columns("B:BQ").EntireColumn.Hidden = False
Columns("D:AB").EntireColumn.Hidden = True
Columns("AF:BO").EntireColumn.Hidden = True
Case "F"
Columns("B:BP").EntireColumn.Hidden = False
Columns("E:AE").EntireColumn.Hidden = True
Columns("AN:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "G"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BJ").EntireColumn.Hidden = True
Columns("BL:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "H"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BJ").EntireColumn.Hidden = True
Columns("BL:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "I"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "J"
Columns("B:BP").EntireColumn.Hidden = False
Columns("E:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "K"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "L"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "M"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "N"
Columns("B:BP").EntireColumn.Hidden = False
Columns("E:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "O"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BJ").EntireColumn.Hidden = True
Columns("BM:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "P"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:AM").EntireColumn.Hidden = True
Columns("AO:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "Q"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BL").EntireColumn.Hidden = True
Columns("BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "R"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:AN").EntireColumn.Hidden = True
Columns("AP:BM").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "S"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:AO").EntireColumn.Hidden = True
Columns("AQ:BM").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "T"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:AN").EntireColumn.Hidden = True
Columns("AP:BM").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "U"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:AP").EntireColumn.Hidden = True
Columns("BB:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
Case "V"
Columns("B:BP").EntireColumn.Hidden = False
Columns("F:BA").EntireColumn.Hidden = True
Columns("BK:BN").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
'do nothing
End Select
Next t
ActiveWindow.Zoom = 100 '<<<
End If
safe_exit:
Application.EnableEvents = True
End Sub
这是公式:
=IF(IFERROR(INDEX('XXXXX'!B$5:B$9963,MATCH($A5,'XXXXX'!$A$5:$A$9963,0)),"")="","",INDEX('XXXXX'!B$5:B$9963,MATCH($A5,'XXXXX'!$A$5:$A$9963,0)))
该数据库包含大约3000行。文件大小约为7-8MB。我删除了所有公式,但存在相同的延迟。我删除了2000行数据,这使得它更快。但看起来不是很大的差别呢?
该文件通过OneDrive共享,许多用户实时使用它。用户后来提到他根本无法打开文件。它在打开时崩溃了。这可能是一个巧合,一个破碎的链接导致腐败?
答案
请注意,您可以将代码减少到只有一个Intersect(Target, Range("B:B"))
并启动循环并使用Match
函数。这样,您只会隐藏列一次,而不是每次在受影响的范围内出现值A
时。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AffectedRange As Range
Set AffectedRange = Intersect(Target, Range("B:B")) 'intersect olny once and set into variable for re-use
If Not AffectedRange Is Nothing Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim FoundAt As Long
On Error Resume Next
FoundAt = Application.WorksheetFunction.Match("A", AffectedRange, False)
On Error GoTo 0
If FoundAt <> 0 Then
Columns("B:BQ").EntireColumn.Hidden = False
Columns("H:AD").EntireColumn.Hidden = True
Columns("AF:BL").EntireColumn.Hidden = True
Columns("BQ").EntireColumn.Hidden = True
End If
ActiveWindow.Zoom = 100 '<<<
End If
safe_exit:
Application.EnableEvents = True
End Sub
以上是关于插入公式后的宏延迟问题 - Excel的主要内容,如果未能解决你的问题,请参考以下文章