无法运行Worksheet_Change事件并且无法确定原因。单元格B5是数据透视表的一部分,当该值发生变化时,需要调用并运行一个不同的子程序(并且该子程序正在运行)
Private Sub Worksheet_Change(ByVal Target As Range)
'MsgBox ("run private")
Dim WatchRange As Range
Dim IntersectRange As Range
Set WatchRange = Range("b25")
Set IntersectRange = Intersect(Target, WatchRange)
If IntersectRange Is Nothing Then
'MsgBox ("do nothing")
Else
'MsgBox ("run macro")
Call ChangeTitle
End If
End Sub
Sub ChangeTitle()
'
'
Dim Mytitle As Range
Set Mytitle = Worksheets.Item("Current").Range("b25")
ActiveSheet.PivotTables("PVTRatingTech").PivotFields("title").CurrentPage = _
Mytitle.Text
End Sub
答案 0 :(得分:0)
好的,我测试了代码,这段代码可以运行。
将其粘贴到模块
中Public Oldval As Variant
Public NewVal As Variant
将其粘贴到工作簿代码区
中'~~> Replace Sheet1 with the actual sheet name
Private Sub Workbook_Open()
Oldval = Sheets("Sheet1").Range("B5").Value
End Sub
并将其粘贴到工作表代码区
中Private Sub Worksheet_Calculate()
NewVal = Sheets("Sheet1").Range("B5").Value
If Oldval <> NewVal Then
Oldval = NewVal
ChangeTitle
End If
End Sub
Sub ChangeTitle()
Dim Mytitle As Range
Set Mytitle = Worksheets.Item("Current").Range("b25")
ActiveSheet.PivotTables("PVTRatingTech").PivotFields("title").CurrentPage = _
Mytitle.Text
End Sub
保存并关闭工作簿并再次打开它。现在当你刷新你的Pivot,如果B5发生变化,那么“ChangeTitle”将会运行。
希望这是你想要的?
答案 1 :(得分:0)
正如Remnant已经指出的那样,你确实有一个错字。不确定你的意思是B5还是B25。
您的代码没有任何问题。如果B25在目标中,那么您的ChangeTitle
宏将会运行。如果宏未运行,则只需B25不在目标中。
另外,您可以将更改事件中的代码缩减为
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("b25")) Is Nothing Then Call ChangeTitle
End Sub