我有一个工作簿,旨在根据数据透视表数据刷新命名范围。当数据透视表刷新时,将根据数据透视表中显示的新值重置命名范围。
当我尝试将范围设置为命名变量时,我收到错误“对象变量或未设置块变量”,这里有什么我做错了,我看不到吗?
Sub RefreshPivots()
Dim PT1 As PivotTable
Dim NameFirst As Range
Dim Config As Worksheet
'Define Worksheet
Set Config = ThisWorkbook.Worksheets("Config")
'Define Pivot Table Ranges
Set PT1 = Config.PivotTables("Pivottable1")
'Clear Current Filters
PT1.PivotFields("Number").ClearAllFilters
'Refresh Pivots
ThisWorkbook.RefreshAll
'Set Pivot Table Filters
PT1.PivotFields("Number").PivotItems("1").Visible = True
PT1.PivotFields("Number").PivotItems("2").Visible = False
PT1.PivotFields("Number").PivotItems("3").Visible = False
PT1.PivotFields("Number").PivotItems("4").Visible = False
PT1.PivotFields("Number").PivotItems("5").Visible = False
PT1.PivotFields("Number").PivotItems("6").Visible = False
PT1.PivotFields("Number").PivotItems("(blank)").Visible = False
'Remove Old Range
ThisWorkbook.Names("First").Delete
'Define Range for new Name (**This is where the error occurs**)
NameFirst = Config.Range("G4:G" & Config.Range("G" & Rows.count).End(xlUp).Row) **<--Error**
'Apply New Named Range
Range(NameFirst).Name = "First"
End Sub
答案 0 :(得分:3)
尝试设置范围对象。
SET NameFirst = Config.Range("G4:G" & Config.Range("G" & Rows.count).End(xlUp).Row)
NameFirst.Name = "First"