我在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与许多使用它的实时用户共享。此后,用户提到他根本无法打开文件。打开时崩溃。可能是巧合,链接断开导致损坏吗?
答案 0 :(得分:1)
请注意,您可以将代码减少到一个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