How do I suppress Excel message for any pivot table update? - vba

I have created a pivot table to summarize some information and have added some formulas adjacent to the pivot table to do calculations on the numbers included in it. I have created a macro that re-enters the formulas whenever the user changes the size of the pivot table (in the PivotTableUpdate event) by showing or hiding various rows/columns of data.
My problem is that whenever columns of data are added to the pivot table, it asks me "Do you want to replace the contents of the destination cells?" I always click yes, because although the cells will be overwritten when the pivot table expands, the formulas will be re-entered in their correct cell and everything is fixed and formatted properly by the macro.
Therefore, I would like to know where I should put application.displayalerts = false so that it is effective to suppress the message box whenever the user expands the pivot table.

Hi tlewis3348 i think this is what you are looking for
Sub UpdateIt()
Dim iP As Integer
Application.DisplayAlerts = False
For iP = 1 To ActiveSheet.PivotTables.Count
ActiveSheet.PivotTables(iP).RefreshTable
Next
Application.DisplayAlerts = True
End Sub

FWIW This solution worked great for me. I just activated each sheet individually and called the UpdateIt function before doing anything else, e.g.,
Worksheets("RFS Monthly Summary").Activate
UpdateIt
Worksheets("RFS Daily Activity").Activate
UpdateIt

Related

Need to have Excel reapply filter to the active sheet when navigating

I have one Excel sheet with navigation buttons to a variety of Student pages. Another sheet with a schedule. The student pages each of a data table of the schedule with a filter set to their name so that it shows only their values from the schedule data.
When I change the schedule, and navigate to the student page the new data is not automatically filtered.
How to I force Excel to re-apply that filter when the user navigates to the page.
I tried:
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
and
Application.CalculateFull
and
ActiveSheet.AutoFilter.ApplyFilter
without success. I can manually do this by clicking Data > Sort & Filter > Reapply in the Ribbon. I want to code this into the navigation so they don't have to do that.
I was using a cell outside an Excel table as the trigger for filtering the table based on its value. I use a validation list that consisted of the values in the first column of the table to avoid typos. I also learned that the technique above only works when the ActiveCell is in the table you're trying to filter. I added code to keep the ActiveCell visible after the filter is reapplied.
Here's the code that worked for me in the Worksheet_Change event:
If Target = Range("Selected_Defined_Name") Then
' then there was a change to the selected defined name. So Reapply the filter to the Range_Names table
With Target.Parent.ListObjects(1).DataBodyRange
.Cells(1, 1).Activate
ActiveSheet.AutoFilter.ApplyFilter
.SpecialCells(xlCellTypeVisible).Cells(1, 1).Activate
End With
End If
Hope this is helpful to someone!

VBA: Mirror a cell to another sheet, then keep the data after deleting the original

Apologies if this has been answered, but I couldn't find any details of this being asked before. I know it's a vague question without code, but just looking for ideas of how to approach this. Writing VBA code on Excel.
I have a sheet, which contains 20+ columns and 8,000+ rows of data. The sheet is password protected / read only as I do not want users editing the data (the sheet is linked to several other macros). Called 'Master Data Sheet'.
I have created a macro that will filter the Master Data table and then paste the filtered results on another sheet (called 'Filtered Results') and combines data with another separate sheet. The user can view and edit this sheet.
However there are two columns that I want the user to put data in that needs to be duplicated/mirrored in the Master Data Sheet (in those specific columns) automatically. Issue I'm having is that the 'Filtered Results' sheet gets deleted after a period of time, but I still need the user input data for those specific columns to be in the Master Data Sheet.
I'm unsure how to get around the issue of the sheet being deleted but data being retained.
An alternative workaround I had would be a copy/paste button the user could click to copy the unformatted data from the Filtered Sheet to Master Data Sheet. However, I was hoping to find a solution that would just automatically update as the user types (in case they forget to click the button etc.)
Thanks for all the help!
EDIT: Code I'm working with right now. Still stuck on linking filtered cells though.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents=False
If Not Intersect (Target, Range("D1:D1000")) Is Nothing Then
Target.Copy Destination:=Worksheets("Master Data
File").Range(Target.Address)
End If
Application.EnableEvents=True
End Sub
You can use the Worksheet_Change event to check if a specific range changed and then copy/update the other sheet. This way you have the copy/paste automatically when the original data changed.
Example:
If you want to copy range A1:A5 into another sheet whenever its values changed:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then 'range A1:A5 was changed
Target.Copy Destination:=Worksheets("OtherSheet").Range(Target.Address)
End If
End Sub

Saving cell Presets in a dropdown list

At the moment I have created a spreadsheet which takes a bunch of inputs, runs them through a list of formulas, and then spits out the results onto a "report" worksheet.
I've been manually saving each of these reports as separate CSVs but I was hoping for a better method moving forward as it is getting quite tiring to have to open 10 CSVs when i do my monthly reports.
I am looking for a way to start saving all of these reports into a "database". My hope to to have one cell be for an user entry name and for two buttons. One to save the current report under the name entered by the user, and two to remove old records. I would then be able to revisit old entries by selecting them in the dropdown.
I've dabbled with VBA and Macros in the past but this is a little more complicated than what I've dealt with in the past. Looking for some help/direction.
Thanks for your time!
Depending on how your reports need to be used, you might find it satisfactory to simply make your data into one big Excel Table ( Insert Tab > Table ). When you do this, Excel will automatically fill-down any formulas that you enter in a column, and also show the formula using the headers instead of A1-style references.
I use this format, adding Y under Remove from Active List on each line that is already done. Then whenever I save the file or look at it for today's status, I filter out what's old and just look at the new. The other filters enable copy-pasting or printing whatever arrangement I like.
The filters and other things in the table can be referenced in VBA as Sheets("ThisSheet").ListObjects(1), which is an object with a number of useful properties and methods.
For VBA information, read more here: https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables
This is my code for auto-filtering the table to hide inactive items at time of save. You add it at ThisWorkbook in the VBA editor:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").Activate
SelectedCell = ActiveCell.Address 'this saves your screen selection for after the filtering
ActiveSheet.ListObjects(1).Range(1, 1).Select
If ActiveSheet.ListObjects(1).AutoFilter.FilterMode = True Then
ActiveSheet.ListObjects.Item(1).AutoFilter.ShowAllData
End If
A = ActiveSheet.ListObjects(1).Range.Rows(1).Find("Remove from List").Column - _
ActiveSheet.ListObjects(1).Range.Column + 1
ActiveSheet.ListObjects(1).Range.AutoFilter field:=A, Criteria1:="="
Range(SelectedCell).Select
End sub

delete rows without loosing the formula in excel using vba

I have an excel sheet in which there is one table with a drop down and one normal excel cell. I try to clear the contents of the table and I have done it through
range("X").ClearContents
but the problem with it is it is going to clearing the content but I'm able to see the table borders with it.
When I have used
Range("X").Select
Application.DisplayAlerts = False
Selection.Delete
Application.DisplayAlerts = True
Range("X").Select
Selection.ClearContents
It deletes the table (both content and borders) but it I can see the formula in the drop down is missing ie the drop down cell has become normal cell.
Thanks in Advance!
You can use the ClearFormats() method, this will remove the table border.
Also, to clear contents, you do not have to first select the range. Take an advantage of the With statement
With Range("X")
.ClearContents
.ClearFormats
End With
The above should achieve what you're after.

External Data Pull: Pull more specific data or change macro based on data

This question is somewhat difficult to explain, so bear with me.
I am pulling data from a large table for my company and am trying to create a macro to make this data easier to read/understand. The data that is on the site changes every day based on what caused certain failures in our plant, which causes my macro to analyze data that isn't there or wrong cells (due to rows getting shifted/moved/added/removed). Because I don't think that was really clear, here is an example:
The macro says to select cells J5, J13, and J25. These were, when I was creating the macro, the values I wanted to be put in a list. However, when I pulled the data and ran the macro today, these values were in different spots on my sheet (the value for cell J13 is now in J12). This completely messes up all of the analysis and renders my macro / data pull useless.
Is there a way to have the macro select the data more intelligently? Perhaps have it check for the group name, then select the value from the cell next to it? I wish I could word this better... Thanks if you've gotten this far!
Simply put... yes. Here's a code exert for looking for a groupname and getting the adjacent cell:
Dim Group1Range As Range
'Look in ThisWorkbook
With ThisWorkbook
'Look in Sheet1
With .Sheets(1)
'Look in Column I
With .Columns("I:I")
'Find the text Group1
Set Group1Range = .Find(What:="Group1").Offset(0, 1)
End With
End With
End With
'Indicate the address of the found range
Debug.Print Group1Range.Address
End Sub
Now here are ways that you can improve your question:
Explain how you know that cell J13 is no longer valid, and that J12 is now.
Give us some sample data.
Give us your code.
Tell us what your end result would be, possibly with an example.