Worksheet_Change Error with Pivot Table Refresh on diff sheet

samjessek

New Member
Joined
Jan 4, 2013
Messages
3
Hello, I am very new to VBA and any help would be appreciated.

The purpose of this code is to reformat one pivot table upon a change on the same worksheet only. I have placed the code in the individual worksheet object. It works perfectly EXCEPT when I refresh a pivot table on a separate sheet I receive a ‘Run-time Error ‘1004’ Select Method of Range class Failed message.

The line that is highlighted in the debugger is after the interest line “Range("D9:L9").Select”. There will be very light VBA in this workbook and any ugly fix will work. Even something similar to an “IFERROR” that simply exits the sub. I’m confused as to why the sub is even activated if I am on a separate worksheet. Thank you!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    Application.ScreenUpdating = False
    Dim Rng As Range
    Set Rng = Intersect(Target, Range("A10:M500"))
        Range("D9:L9").Select
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
        Range("M9").Select
    With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Columns("D:L").Select
    Selection.ColumnWidth = 16
    Range("A1").Select
End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try this to correct:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)  Application.ScreenUpdating  = False

Dim ws As Object
Dim Rng As Range

If ws.Name <> "Personnel" Then Exit Sub

'place code here

End Sub
 
Upvote 0
Correction, place in the ThisWorkbook Module:

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name <> "Personnel" Then Exit Sub
'
'rest of code
'
End Sub
 
Upvote 0
It worked at preventing the refresh error, but the change event was no longer triggered for my originally targeted sheet. :-(
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top