Nuke_It_Newport
New Member
- Joined
- Nov 17, 2020
- Messages
- 47
- Office Version
- 365
- Platform
- Windows
Hi everyone-
I am curious if there's any method to reference a pivot table (on a separate sheet) that is sourced from the active sheet without calling it explicitly. The context of this is as follows:
In my active sheet, If Range("C2:E2") value is changed, this value is pasted into the associated PivotTable header. I would like to move this code from the Worksheet_Change modules to the Workbook_SheetChange module, but I would need a way to reference the PivotTable that is sourcing from the active sheet.
Here's a sample of the code I have in the Worksheet_Change modules.
Here's the code I've got in Workbook_SheetChange:
Is it better to leave this code in the Worksheet_Change modules? I'm trying to clean up my code.
Thanks!
I am curious if there's any method to reference a pivot table (on a separate sheet) that is sourced from the active sheet without calling it explicitly. The context of this is as follows:
In my active sheet, If Range("C2:E2") value is changed, this value is pasted into the associated PivotTable header. I would like to move this code from the Worksheet_Change modules to the Workbook_SheetChange module, but I would need a way to reference the PivotTable that is sourcing from the active sheet.
Here's a sample of the code I have in the Worksheet_Change modules.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C2:E2")) Is Nothing Then
If Len(Target) = 0 Then
MsgBox "Worksheet title cannot be left blank. Reverting to previous title...", vbOKOnly, vbCritical
Application.Undo
Else
On Error GoTo Err_Handler
ActiveSheet.Name = Target.Value
With ThisWorkbook.Sheets("~WKSP 1 PT").PageSetup
.CenterHeader.Text = Target.Value
End With
End If
End If
Exit Sub
Err_Handler:
If Err.Number = 1004 Then
MsgBox "The sheet could not be renamed. Please check " & vbCrLf & _
"that you did not use illegal characters."
Application.Undo
Resume Next
End If
End Sub
Here's the code I've got in Workbook_SheetChange:
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Not Left(Trim(ActiveSheet.Name), 1) = "~" Then
If Not Intersect(Target, Sh.Range("C2:E2")) Is Nothing Then
If Len(Target.Text) = 0 Then
MsgBox "Worksheet title cannot be left blank. Reverting to previous title...", vbOKOnly, vbCritical
Application.Undo
Else
On Error GoTo Err_Handler
ActiveSheet.Name = Target.Value
' With ***NEED TO REFERENCE PIVOT TABLE HERE...
' .CenterHeader.Text = Target.Value
' End With
End If
End If
End If
Exit Sub
Err_Handler:
If Err.Number = 1004 Then
Debug.Print "Worksheet renaming error. Check for illegal characters."
MsgBox "The sheet could not be renamed. Please check " & vbCrLf & _
"that you did not use illegal characters."
Application.Undo
Resume Next
End If
Application.EnableEvents = False
ThisWorkbook.RefreshAll
Application.EnableEvents = True
End Sub
Is it better to leave this code in the Worksheet_Change modules? I'm trying to clean up my code.
Thanks!