我目前有一个宏来自动筛选我的数据。我无法让它在单元更改时自动运行。我希望宏在单元格中的值发生更改时运行。会感激任何形式的帮助。我想知道是否问题在于我将代码放在哪张工作表中。高级过滤器宏不能自动工作
我的代码如下。
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