Excel VBA不更新数据透视表日期范围
我不知道哪里出了问题,但它不起作用,也没有给出任何错误。我正在尝试使用单元格值更新透视表,这些单元格值应过滤单元格G3和G4中给定的日期范围。但不幸的是,没有更新表 下面是我正在使用的VBAExcel VBA不更新数据透视表日期范围,excel,vba,Excel,Vba,我不知道哪里出了问题,但它不起作用,也没有给出任何错误。我正在尝试使用单元格值更新透视表,这些单元格值应过滤单元格G3和G4中给定的日期范围。但不幸的是,没有更新表 下面是我正在使用的VBA Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'This line stops the worksheet updating on every change, it only updates when cell 'G3 or G4
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'G3 or G4 is touched
If Intersect(Target, Range("G3:G4")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim EDate As String
Dim SDate As String
'Here you amend to suit your data
Set pt = Worksheets("Report").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Date")
SDate = Worksheets("Report").Range("G3").Value
EDate = Worksheets("Report").Range("G4").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.PivotFilters.Add Type:=xlDateBetween, Value1:=SDate, Value2:=EDate
pt.RefreshTable
End Sub
我自己解决了。在下面发布答案,可能是有人帮助的
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'G3 or G4 is touched
If Intersect(Target, Range("G3:G4")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim EDate As String
Dim SDate As String
'Here you amend to suit your data
Set pt = Worksheets("Report").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Date")
SDate = Worksheets("Report").Range("G3").Value
EDate = Worksheets("Report").Range("G4").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.PivotFilters.Add Type:=xlDateBetween, Value1:=SDate, Value2:=EDate
pt.RefreshTable
End With
End Sub
它对我有用。我注意到您缺少一条以结尾的
语句。另外,您是否将代码放入了报告的工作表模块中?是的。我也犯了同样的错误。插入结束与,现在它的工作。谢谢