I a using Excel 2013.
My purpose is to be certain that data is not in a filtered state when the workbook is saved. To do that I am trying to use the BeforeSave Event.
First a little background on my approach:
Simply using the code "ActiveSheet.ShowAllData" fails if the ActiveSheet is not currently in a filtered state. Simply looking for "True" or "False" regarding FilterMode/AutoFIlterMode was not producing results so I created 2 cells. One that counts Total Data Rows and One that counts Data Rows in Current Filtered/Unfiltered state. If the FilteredDataRows cell value is < TotalDataRows cell value then Filtering is on and must be turned off. Pretty basic. I then wrote the following code:
[BEGIN CODE]
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim TotalDataRows As Integer
Dim FilteredDataRows As Integer
TotalDataRows = Range("TotalDataRows")
FilteredDataRows = Range("FilteredDataRows")
If FilteredDataRows < TotalDataRows Then
ActiveWorkbook.Worksheets("Data").ShowAllData
Cancel = True
End If
Worksheets("Title").Select
Range("A1").Select
End Sub
[END CODE]
Here are the 2 problems.
1. If, in the VBA code title I remove "ByVal SaveAsUI As Boolean, Cancel As Boolean" from between the "()", I can run the macro from the VBA editor and it performs the task correctly. This does not lead to saving the file of course but the code does what it should. If I leave that wording in then I cannot even run the macro. I don't get any message only a tone and the macro does nothing.
2. When I select the Save Icon it merely saves the file as is and does not recognize the existence of the code with either title variation given in 1. above. This is of course the primary issue.
Any help would be greatly appreciated.
Bookmarks