我目前有一個宏來自動篩選我的數據。我無法讓它在單元更改時自動運行。我希望宏在單元格中的值發生更改時運行。會感激任何形式的幫助。我想知道是否問題在於我將代碼放在哪張工作表中。高級過濾器宏不能自動工作
我的代碼如下。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed. In this case,
' Cell B2 will be changed when a value is selected on
' another worksheet.
Set KeyCells = Range("B1:B2")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Perform advanced filter on data
' Place your code here.
Range("B4:H976").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("SalesByLocation!Criteria"), Unique:=False
End If
End Sub
你試過調試它嗎?在'Set KeyCells = Range(「B1:B2」)'設置一個斷點,看看是否有命中。 – nunzabar 2013-03-11 19:44:03
你好,你是什麼意思設置一個斷點? – sychia2010sis 2013-03-11 19:57:21
要設置斷點,請將光標置於'Set KeyCells = ...'行並按F9。當代碼被執行時,即事件觸發,VBA將停止執行 - 您可以使用F8單步執行代碼以查看發生了什麼。試一試,讓使用知道它是如何去的。 PS:請在任何評論中使用@username,以便在您的回覆中收到通知! – 2013-03-11 20:20:01