2017-01-13 35 views
1

我正在製作一個表格,列出全年的日子。這些行將由不同的用戶手動填充,目的是在整個一年中記錄這些值。列是特定於一年中的每一天,因此需要大量的滾動來查找特定範圍。Excel VBA:隱藏兩個給定日期之間的列

我想實現的是有兩個單元格可以填充兩個日期,一個開始日期和結束日期,當這些被輸入時,所有其他列都隱藏起來。

我發現了一種方法來隱藏一個單元格中給定日期之前的列,但像一些幫助合併一種方法來隱藏另一個單元格中給定日期後的列。在這種情況下細胞E35

該方法的VBA代碼到目前爲止是:

Private Sub Worksheet_Change(ByVal Target As Range) 
'Updateby Extendoffice 20160725 
    Dim xCell As Range 
    If Target.Address <> Range("E34").Address Then Exit Sub 
    Application.ScreenUpdating = False 
    For Each xCell In Range("H1:NG1") 
     xCell.EntireColumn.Hidden = (xCell.Value < Target.Value) 
    Next 
    Application.ScreenUpdating = True 
End Sub 

參考畫面的位置: Example

預先感謝您// - [R

回答

2
Private Sub Worksheet_Change(ByVal Target As Range) 
'Updateby Extendoffice 20160725 
    Dim xCell As Range 
    If Target.Address <> Range("E34").Address AND Target.Address <> Range("E35").Address Then Exit Sub 
    Application.ScreenUpdating = False 
    For Each xCell In Range("G1:NG1") 
    xCell.EntireColumn.Hidden = (xCell.Value < Range("E34").Value or xCell.Value > Range("E35").Value) 
    Next 
    Application.ScreenUpdating = True 
End Sub 
3

對@dgorti的後續回答,在未來的情況下,當您想要監視多個範圍的工作表時,可以使用:

Private Sub Worksheet_Change(ByVal Target As Range) 

If Not Intersect(Target, Range("E34:E35")) Is Nothing Then 
    ' perform your code hewe 

Else 
    ' you can put your Exit Sub here 
End If 

End Sub 
相關問題