对单元格值更改应用macros:1004错误

我试图从一个单元格的值更改时应用macros。 我在仪表板中有这样的代码:

Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Target.Worksheet.Range("FilterChoice")) Is Nothing Then Call ApplyDashboardFilter End Sub 

触发器工作正常,macros执行后,但不知道为什么我得到了高级filterfunction的错误:“应用程序定义或对象定义的错误”

 Option Explicit Sub ApplyDashboardFilter() Dim rng As Range Dim filterName As String Dim tableName As String filterName = "Filter" & Replace(Sheets("Dashboard").Range("FilterChoice").Value, " ", "") tableName = filterName + "[#All]" Sheets("Dashboard").Activate Sheets("Dashboard").Columns("A:AN").Cells.Clear Sheets("Critical Flows").Range("ClosingFlows[#All]").AdvancedFilter Action:=xlFilterCopy _ , CriteriaRange:=Sheets(filterName).Range(tableName) _ , CopyToRange:=Sheets("Dashboard").Range("A1"), Unique:=False Set rng = Range(Range("A1"), Range("A1").CurrentRegion) ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = _ "Flows" & filterName ActiveSheet.ListObjects("Flows" & filterName).TableStyle = "TableStyleMedium3" If Sheets("Dashboard").Range("FilterChoice").Value = "Orchestrated" Then Call ApplyFlormulaRunbookName End If End Sub 

该macros由Dashboard表单上的button触发。

我错过了什么吗?

提前致谢,

编辑:

那么奇怪的事情发生了。 rest一下,我刚刚重新打开文件, 它工作 。 我怀疑ActiveSheet发生了什么事情和/或与另一个工作簿冲突,因为我正在玩2其他工作簿和总计10张。

可能吗 ?

我添加了一个答案作为评论不会让我格式正确。 这段代码只是引用表单,而不是select它们:

 Sub ApplyDashboardFilter() Dim rng As Range Dim filterName As String Dim tableName As String Dim wrkShtDash As Worksheet Dim wrkShtFlows As Worksheet Set wrkShtDash = ThisWorkbook.Worksheets("Dashboard") Set wrkShtFlows = ThisWorkbook.Worksheets("Critical Flows") filterName = "Filter" & Replace(wrkShtDash.Range("FilterChoice").Value, " ", "") tableName = filterName + "[#All]" wrkShtDash.Columns("A:AN").Cells.Clear wrkShtFlows.Range("ClosingFlows[#All]").AdvancedFilter Action:=xlFilterCopy _ , CriteriaRange:=ThisWorkbook.Worksheets(filterName).Range(tableName) _ , CopyToRange:=wrkShtDash.Range("A1"), Unique:=False Set rng = wrkShtDash.Range(wrkShtDash.Range("A1"), wrkShtDash.Range("A1").CurrentRegion) wrkShtDash.ListObjects.Add(xlSrcRange, rng, , xlYes).Name = _ "Flows" & filterName wrkShtDash.ListObjects("Flows" & filterName).TableStyle = "TableStyleMedium3" If wrkShtDash.Range("FilterChoice").Value = "Orchestrated" Then Call ApplyFlormulaRunbookName 'Spelt correctly? End If End Sub 

注意:我没有testing代码,它只是显示你在工作之前不必激活工作表,并明确了它正在处理哪个文件或工作表 – ThisWorkbook意味着VBA代码所在的文件。