Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Update Pivot data with VBA

0

Hello dears,

Your help is highly appreciated as attached sample that i need if the data changed in (Data sheets) its upated after run VBA to be with the new data in (Top10 sheet) cause if the main data changed, VBA code already saved Top10 named that applied once VBA code recorded, and not collect updated data

thanks in advanced

Answer
Discuss

Discussion

Hi Luka.

Please see my answer below (and you can comment in the Discussion section below it- kindly do not use Answer).

Also, please EDIT your question to correct the spelling of VBA in the title (I can't do it for you). Thanks.
John_Ru (rep: 6142) Aug 14, '22 at 7:12 am
Thanks Luka
John_Ru (rep: 6142) Aug 14, '22 at 10:15 am
Add to Discussion

Answers

0
Selected Answer

Hi Luka and welcome to the Forum

I hope I've understood your question correctly...

Your existing macros were recorded using the Record Macro feature - I have not tried to optimise them but in the attached revised file, I offer a solution which leaves them in tact and working.

Excel has "events" (search the Tutorials section on that) and we can use the Worksheet_Change event- that triggers the macro below if a value is changed (or re-entered) in your Data sheet (the code is behind that sheet in VB Project Explorer).

I've commented the code to explain what happens but essentially it checks if a data value was changed. If so it runs your two macros and moves to the Pivot sheet to show the new Top 10.

Private Sub Worksheet_Change(ByVal Target As Range)

' do nothing it a cell outside the data table is changed
If Intersect(Target, Range("A1").CurrentRegion) Is Nothing Then Exit Sub

'prevent re-triggering this macro...
Application.EnableEvents = False
' ... and screen swapping
Application.ScreenUpdating = False


'run the clear and top10 macros
Call Clear
Call Top_10
' show new pivot
Worksheets("Pivot").Activate
'restore
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

Currently your data sheet is left in the filtered mode but my version uses a workbook event (triggered when sheets are selected) to clear the filter is the user returns to the Data sheet. Here's the code (under ThisWorkbook in VB Project Explorer):

Private Sub Workbook_SheetActivate(ByVal Sh As Object)

If Sh.Name = "Data" And Sh.AutoFilterMode = True Then
    ' if data sheet re-selected, clear filters
    Sh.AutoFilterMode = False
End If

End Sub

That will leave the Pivot sheet unchanged (but you can delete it using your Clear button).

REVISION:

Given your additional requirement to copy the PivotTable to the sheet "Top10", in the second attached file, I've extended the code above to do that. Changes and comments are in bold:

Private Sub Worksheet_Change(ByVal Target As Range)

' do nothing it a cell outside the data table is changed
If Intersect(Target, Range("A1").CurrentRegion) Is Nothing Then Exit Sub

'prevent re-triggering this macro...
Application.EnableEvents = False
' ... and screen swapping
Application.ScreenUpdating = False


'run the clear and top10 macros
Call Clear
Call Top_10

'clear and populate Top10 sheet
With Worksheets("Top10")
    .UsedRange.Clear
    ' copy new pivot
    Worksheets("Pivot").PivotTables("PivotTable1").TableRange2.Copy
    'paste values to clean sheet
    .Range("A1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    'adjust some values and add borders
    .Range("A1:B1") = Array("Name On Card", "Tx Total")
    .Columns(2).NumberFormat = "#,##0.00"
    .Columns("A:B").AutoFit
    .UsedRange.Borders.LineStyle = xlContinuous
    ' show Top10
    .Activate
    .Range("D1").Select
End With

'restore
Application.EnableEvents = True
Application.ScreenUpdating = True

End Sub

Note that it would be nice to hide your "Pivot" sheet but your recorded macro prevents that (and would take a lot of effort to mofiy that to avoid selecting ranges- which your Top_10 macro deos a lot).

Hope this fixes your problem- if so, please remember to mark this Answer as Selected.

Discuss

Discussion

Thanks John for your effort, but can't download attached file for checking, its directed me to below link:
https://www.teachexcel.com/talk/
Luka_Rashed (rep: 2) Aug 14, '22 at 10:00 am
@Luka
Sorry about that. Recent spam attacks meant Don had to update the site so that may be the reason you were redirected...
@Don
I had Luka's experience too- clicked on the file in my answer above and went to an  unrelated, empty page. Can you fix it please?
John_Ru (rep: 6142) Aug 14, '22 at 10:13 am
@Don
any update dear

thanks for your kindly support
Luka_Rashed (rep: 2) Aug 15, '22 at 11:12 am
Luka

Don is a busy man (especially when spammers have made this site difficult to manage recently) but I hope he fixes this soon.

I tried to re-attach the file and repeat it (as a second file) but neither works, sorry.

You could copy the macros into your file, adding the first "behind" sheet1 in VB Project Explorer and the second under ThisWorkbook.  Your file should then work as I described it above.
John_Ru (rep: 6142) Aug 16, '22 at 5:51 am
Luka

Don has now fixed the file download feature- please see answer and comment. I will delete the second file now/
John_Ru (rep: 6142) Aug 16, '22 at 3:01 pm
@John_Ru
thanks for your effort t fix it, so its already updated of Pivot sheet if the main data changed but not reflected to final sheet ( Top 10 ) its left changed data row empty
Luka_Rashed (rep: 2) Aug 18, '22 at 10:54 am
Luka 

I can't check your file on my mobile phone now so I don't understand your comment above "its left changed data row empty" -  please explain. 

I'll then try to look at that tomorrow but your question wasn't quite clear to me to be honest. 
John_Ru (rep: 6142) Aug 18, '22 at 11:35 am
Luka. I need your clarification before I do any more to this file.

Is the Top10 sheet to be like the Pivot sheet (but not a pivot table, just the values)?
John_Ru (rep: 6142) Aug 19, '22 at 1:39 pm
Dear John
     Your effor is very appreciated,

"Is the Top10 sheet to be like the Pivot sheet (but not a pivot table, just the values)?"

that's my point
Luka_Rashed (rep: 2) Aug 19, '22 at 5:12 pm
Okay Luka but it helps if you are clear at first so that I don't have to guess! You asked how to update a pivot table (and I replied). 

I will try to change it over the weekend.
John_Ru (rep: 6142) Aug 19, '22 at 6:55 pm
Luka. Please see the revision to my Answer and the second file.

If you have further questions or requirements, please raise them as new, separate questions.
John_Ru (rep: 6142) Aug 20, '22 at 6:15 am
Thanks John_Ru for your effort.
Luka_Rashed (rep: 2) Aug 20, '22 at 5:33 pm
Glad that worked for you,  Luka.  Thanks for selecting my answer. 
John_Ru (rep: 6142) Aug 20, '22 at 6:10 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login