PDA

View Full Version : Solved: Control the execution of macros



rajagopal
06-19-2007, 11:03 PM
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

geekgirlau
06-19-2007, 11:21 PM
Which workbook event are you using? If you are using the Before Close event, you can do something like this:


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

rajagopal
06-19-2007, 11:26 PM
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

Bob Phillips
06-20-2007, 12:36 AM
In ThisWorkbook



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

rajagopal
06-20-2007, 12:43 AM
Thanks a lot. It works fine.
May i know the explanation of mchangeflag code?

Regards

Bob Phillips
06-20-2007, 12:46 AM
It just tells the close routine whether anything was changed.