设置导致“对象variables或块variables未设置”错误的dynamic范围

我有一个工作簿,旨在刷新基于数据透视表数据的命名范围。 数据透视表刷新时,将根据数据透视表中出现的新值重置命名的范围。

当我尝试将范围设置为一个命名variables时,出现“对象variables或块variables未设置”的错误,是否有我在这里做错的东西,我看不到?

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 

尝试设置范围对象。

 SET NameFirst = Config.Range("G4:G" & Config.Range("G" & Rows.count).End(xlUp).Row) NameFirst.Name = "First"