Consulting

Results 1 to 6 of 6

Thread: Solved: Control the execution of macros

  1. #1

    Solved: Control the execution of macros

    I've workbook event codes in an excel file say abc.xls
    Consider that abc.xls is opened by the user but not modified. In this case, the macros should not get executed.
    How this can be done?

    Regards

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Which workbook event are you using? If you are using the Before Close event, you can do something like this:

    [VBA]
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ActiveWorkbook.Saved = False Then
    ' rest of code
    End If
    End Sub

    [/VBA]

  3. #3
    Yes. I use Before close event. Your code meet my requirement when the user do not save the file.
    If the user save the empty file accidentally, then the macros will be executed.
    Is there any other way to instruct the macros to get executed only when the sheet is updated??

    Regards

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    In ThisWorkbook

    [vba]

    Private mChangeFlag As Boolean

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If mChangeFlag Then
    'run your code
    End If
    End Sub

    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    mChangeFlag = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Thanks a lot. It works fine.
    May i know the explanation of mchangeflag code?

    Regards

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It just tells the close routine whether anything was changed.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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