Application.EnableEvents not working

miketastic

New Member
Joined
Apr 8, 2015
Messages
14
Hello,

I was using the following code successfully to disable the popup box asking if the user would like to overwrite the file of the same name. However, when I changed the Path to a location on a network drive (not only does the macro run painfully slow), the application.enableevents stopped working. It doesn't disable anything and I changed nothing about the code other than the path from "P:" to "\\10.1.555.555\stuff"

I'm still pretty new at this stuff, I'm sure it's something I'm doing wrong. Thanks for you help! :)

Code:
Dim MyPath As String
Dim MyFileName As String
MyPath = "P:"
MyFileName = "CSVUPLOAD"
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
Sheets("CSV UPLOAD").Copy
Application.EnableEvents = FalseWith ActiveWorkbook
    .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
End With
Application.EnableEvents = True
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello,

I was using the following code successfully to disable the popup box asking if the user would like to overwrite the file of the same name. However, when I changed the Path to a location on a network drive (not only does the macro run painfully slow), the application.enableevents stopped working. It doesn't disable anything and I changed nothing about the code other than the path from "P:" to "\\10.1.555.555\stuff"

I'm still pretty new at this stuff, I'm sure it's something I'm doing wrong. Thanks for you help! :)

Code:
Dim MyPath As String
Dim MyFileName As String
MyPath = "P:"
MyFileName = "CSVUPLOAD"
If Not Right(MyPath, 1) = "\" Then MyPath = MyPath & "\"
If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
Sheets("CSV UPLOAD").Copy
[COLOR="#FF0000"]Application.EnableEvents = FalseWith ActiveWorkbook[/COLOR]
    .SaveAs Filename:=MyPath & MyFileName, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
End With
Application.EnableEvents = True

The line highlighted in red will prevent enableevents from working. If that's really the way it appears in your code put a line break before "With".
 
Upvote 0
Hi,

What can happen with EnableEvents is that they can be disabled and the macro can fail before the re-enabling takes place. I usually keep a macro the just re-enables the events handy while I am debugging a macro so that I can re-enable them if that happens.

Also, it may be worthwhile adding an ON ERROR statement that skips to the re-enable command in case of an error.


Code:
Sub myMacro()

    On Error GoTo ReEnable
    Application.EnableEvents = False

    '
    ' More code goes here
    '

ReEnable:
    Application.EnableEvents = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top