+ Reply to Thread
Results 1 to 14 of 14

BeforeSave Macro Not Working

Hybrid View

  1. #1
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    BeforeSave Macro Not Working

    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.

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: BeforeSave Macro Not Working

    Try

    On Error Resume Next
    ActiveWorkbook.Worksheets("Data").ShowAllData
    On Error GoTo 0
    Martin

  3. #3
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: BeforeSave Macro Not Working

    First, Thanks for the quick response. That is a great fix for the "ActiveSheet.ShowAllData" problem.
    However, the main problem is that when I select the Save Icon from the file menu it doesn't seem to recognize the existence of the code at all. I was only running into the problem you clear up when I ran the code from the VBA editor. If you can figure why it's not even seeing the commands I will probably use your solution above in place of my coding.

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: BeforeSave Macro Not Working

    Hi DMumme

    This works for me in 2010
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If Sheets("Data").AutoFilterMode = True Then
        On Error Resume Next
        Sheets("Data").ShowAllData
        On Error GoTo 0
      End If
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: BeforeSave Macro Not Working

    Thanks! - Nope.
    Save just doesn't recognize the existence of the Macro. Does the Macro need to be in a special location or under Option Explicit or...? And if so how does that work?

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: BeforeSave Macro Not Working

    Hi DMumme

    In ThisWorkBook Module

    Does the Macro need to be in a special location
    How does it work?

    The Workbook Code calls THAT Module BEFORE it closes.
    Last edited by jaslake; 12-13-2014 at 08:34 PM.

  7. #7
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: BeforeSave Macro Not Working

    Thanks!
    Yes it is located in the same workbook that the sheet comprising the data is.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: BeforeSave Macro Not Working

    Hi DMumme

    Go to VBA (ALT + F11).

    In the Left Hand Panel select ThisWorkbook.

    In the Right Hand Panel paste this Code.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If Sheets("Data").AutoFilterMode = True Then
        On Error Resume Next
        Sheets("Data").ShowAllData
        On Error GoTo 0
      End If
    End Sub
    Go back to the Worksheet (ALT + F11). Save the Workbook and close.

    Open up the Workbook and do your stuff...let me know of issues.

  9. #9
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: BeforeSave Macro Not Working

    No difference.
    Have even created a spreadsheet with just your macro and very basic data in it. Doesn't work there either.
    Save just doesn't look a the
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Macro.

  10. #10
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: BeforeSave Macro Not Working

    Hi DMumme

    If you'll do this I'll see if I can help.

    Please attach a sample file that represents what you have. The structure of your attachment should be the same structure as your actual data. Any proprietary information should be changed.

    Include in the attachment any code you're currently using (whether it works or not) and, if appropriate, an "After" worksheet that demonstrates what you wish the output to be.

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the "Add Files"... button to locate your file for uploading.
    6. This will open a new window File Upload...Click "Select Files"
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the "Upload Files" button and wait until the file has uploaded.
    10. Click the "Done" Button.

  11. #11
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: BeforeSave Macro Not Working

    Thanks for all your work!
    Attached Files Attached Files

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: BeforeSave Macro Not Working

    Hi DMumme

    See if this Code in the attached works for you
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      If Sheets("Data").AutoFilterMode = True Then
        On Error Resume Next
        Sheets("Data").ShowAllData
        On Error GoTo 0
      End If
      Application.EnableEvents = False
      ActiveWorkbook.Save
      Application.EnableEvents = True
      Application.DisplayAlerts = False
      ActiveWorkbook.Close
      Application.DisplayAlerts = True
    End Sub
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    03-30-2014
    Location
    Glendale, Arizona
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: BeforeSave Macro Not Working

    Sorry, it took me so long to get back. Took that family out for dinner.
    Nothing happens when I press Save - other than the file is saved as is. I assume you tried this macro in the workbook I sent you and it must have worked or you wouldn't have suggested the solution to me. This is a puzzler. Are you using 2013 or a different version? Won't visit this again until tomorrow. Have a nice night.

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: BeforeSave Macro Not Working

    Hi DMumme

    The Code has been tested and performs as expected in 2007 and 2010. I don't have access to 2013...sorry.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] beforesave macro causes excel to crash
    By carrob in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-13-2012, 10:06 AM
  2. Replies: 1
    Last Post: 03-08-2012, 03:03 AM
  3. BeforeSave not working properly
    By kingdt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-09-2007, 10:43 AM
  4. Trying to run macro on the BeforeSave event
    By dmagoo22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-05-2006, 04:40 PM
  5. Replies: 9
    Last Post: 07-05-2005, 08:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1