2013-03-26 27 views
0

以下代碼應用於自動篩選從日期條件中工作表設定「‘的總計’!B1」幾個工作表之前施加三行,施加到工作表範圍A6,其被格式化爲日期。第1-5行包含標題和公式。但是,行3-5隨着從A6開始的過濾行一起消失。任何人都能看到爲什麼據我所知,A6行上面的所有內容都應該保持可見。再次感謝你的幫助。爲什麼自動篩選被指定範圍

子ApplyFilterDate()

Dim Ws As Worksheet 
    Application.ScreenUpdating = False 'Turn off ScreenUpdating to speed filtering 
     For Each Ws In ActiveWorkbook.Worksheets 
      If Ws.Name <> "Grand Totals" Then 
       Ws.Activate 
       Ws.AutoFilterMode = False 'Remove any existing filters 
       Ws.Range("A6").AutoFilter Field:=1, Criteria1:=Range("'Grand Totals'!B1").Text 
       Range("G2").Activate 
       Center_it 'Puts next data entry cell in approximate center of screen 
      End If 
     Next 
    Sheet1.Activate 
    Range("B2").ClearContents 
    Range("B1").Interior.ColorIndex = 3 'Set color of cell showing filter date 
    Range("B1").Activate 
    Application.ScreenUpdating = True 'Turn on ScreenUpdating 

末次

回答

1

你問自動篩選只在單元格A6操作。這沒有意義,所以Excel擴展選擇以包括周圍的單元格。您需要指定應用自動過濾器的範圍。

+0

謝謝,這工作。我曾經看過幾個例子,很多人只有一列中的第一個單元格被引用,所以我認爲這可能就足夠了。男孩,我有很多東西要學習!如果您的範圍超過1列,Field參數用於選擇要過濾的列? – user2161965 2013-03-29 14:56:57

+0

Excel的VBA不是簡單的對象模型。我的建議是瞭解** Range **對象,並在接受宏記錄器提供的內容之前三思。是的,你對* Field *參數是正確的。如果你認爲它比另一個更好,請點擊勾號接受我的答案。謝謝 – grahamj42 2013-03-29 15:04:47

1

使用以下語法,以避免您的問題:

Ws.Range("A6:e6").AutoFilter '... and so on with exact range address