Friday, March 6, 2020

Auto Save Option In Excel Via Visual Basic

  • Open a new excel file and then press ALT Key + F11
  • After press this key, you will see a new window open of visual basic. 
  • like this:-
  • and then select This Workbook and press enter.
  • and select Workbook in first Tab and BeforeClose Select in 2nd Tab
    • and then start writing the code for of auto save in excel.
    • “ThisWorkbook.Save” will automatically save the workbook and save it as a macro-enabled workbook to run the code like this:
    • Save this workbook as a macro-enabled and close the workbook. Then we can observe that display alert asking for save or don’t save won’t get pop out but some other warning alert will pop out which is as below:

    Example 2.8
    • Once you click on OK, the workbook with changes will get auto saved. We can also disable this alert by using some code in VBA which saves a workbook automatically in a simple way.
    • Like this,
    •  VBA Example 2.9
    • These warning messages will come through an application so we take “Application.DisplayAlerts = False”. This means any sort of display alert will not pop out once this line is included in the code. But it also important to include a line of “Application.DisplayAlerts = True” as this helps when something important display alert to pop out.
    • Now try to make changes and close the workbook, we can observe that none of the display alerts will pop out but changes will get saved. This operation will help us in order to save our work though we unexpectedly close the workbook without saving it.

    No comments:

    Post a Comment