Saving as dialog box. Application.DisplayAlerts not working

Serafin54

Board Regular
Joined
Apr 11, 2014
Messages
165
Office Version
  1. 2016
Platform
  1. Windows
Hello,

can someone help me with suppressing the "saving as \\xyz\branch\sh\Analyst Use Only\UserLog.xls" dialog box that appears when saving?

I have attempted to add Application.DisplayAlerts=FALSE and Application.DisplayAlerts=True to the code but it doesn't seem to make any difference. Thank you.



VBA Code:
'Create UserLog and record metrics
    Dim filePath As String
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lastRow As Long
    Application.ScreenUpdating = False

    ' Define the file path
    filePath = "\\xyz\branch\sh\Analyst Use Only\UserLog.xlsx"
    
    ' Check if the file exists
    If Dir(filePath) = "" Then
        ' Create a new workbook if the file doesn't exist
        Set wb = Workbooks.Add
        wb.SaveAs filePath
        wb.Close
    End If
    
    ' Open the UserLog workbook
    Set wb = Workbooks.Open(filePath)
    
    ' Add a new worksheet named "UserLog" if it doesn't exist
    On Error Resume Next
    Set ws = wb.Sheets("UserLog")
    On Error GoTo 0
    If ws Is Nothing Then
        Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
        ws.Name = "UserLog"
        ' Add headers
        With ws
            .Cells(1, 1) = "Username"
            .Cells(1, 2) = "Date and Time"

        End With
    End If
    
    ' Find the last used row in column A
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' Unprotect sheet and write username and current date and time to the next row
    With ws
        .Unprotect "incorrect"
        .Cells(lastRow + 1, 1) = Environ("Username")
        .Cells(lastRow + 1, 2) = Now

    End With
    
    ' Autofit all columns
    ws.Columns.AutoFit
    
    'Re-protect sheet
    ws.Protect "incorrect", UserInterfaceOnly:=True
    
    ' Save the workbook
    wb.Save
    
    ' Close the workbook
    
    wb.Close
    Application.ScreenUpdating = True
    ' Release memory
    Set wb = Nothing
    Set ws = Nothing

Application.ScreenUpdating = True

'Process Complete messaging
    MsgBox "Process Complete."
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
In addition to @DanteAmor 's recommendation, I notice that when a filepath does not exist, you are creating, saving, closing, then reopening the new workbook. You could simplify by not closing it.

VBA Code:
    ' Check if the file exists
    If Dir(filePath) = "" Then
        ' Create a new workbook if the file doesn't exist
        Set wb = Workbooks.Add
        Application.DisplayAlerts = False
        wb.SaveAs filePath
        'wb.Close                                      'no need to close
        Application.DisplayAlerts = True
    Else
        ' Open the UserLog workbook
        Set wb = Workbooks.Open(filePath)
    End If
    
    ' Add a new worksheet named "UserLog" if it doesn't exist
    On Error Resume Next
    Set ws = wb.Sheets("UserLog")
    On Error GoTo 0
 
Upvote 0
What the message says?



Try at the beginning of the code:

Application.DisplayAlerts=False
it was a message box stating "Saving As" followed by the file name.

I did try the application.displayalerts route but it didn't stop it from appearing.
 
Upvote 0
In addition to @DanteAmor 's recommendation, I notice that when a filepath does not exist, you are creating, saving, closing, then reopening the new workbook. You could simplify by not closing it.

VBA Code:
    ' Check if the file exists
    If Dir(filePath) = "" Then
        ' Create a new workbook if the file doesn't exist
        Set wb = Workbooks.Add
        Application.DisplayAlerts = False
        wb.SaveAs filePath
        'wb.Close                                      'no need to close
        Application.DisplayAlerts = True
    Else
        ' Open the UserLog workbook
        Set wb = Workbooks.Open(filePath)
    End If
   
    ' Add a new worksheet named "UserLog" if it doesn't exist
    On Error Resume Next
    Set ws = wb.Sheets("UserLog")
    On Error GoTo 0
thank you. i will give this a try.
 
Upvote 0
it was a message box stating "Saving As" followed by the file name.

I did try the application.displayalerts route but it didn't stop it from appearing.
this unfortunately didn't work either and actually seemed to slow the code down. I think I might just live with the notification as the folder is locked and will just add a footnote to the add-in itself to inform the users of this notification. I appreciate the help though. Just not sure how much time i want to spend skinning a cat when it doesn't interfere with the end result. Just a nuissance more than anything really.
 
Upvote 0
Application.DisplayAlerts=False only hides Excel alerts, if it is a Windows alert then it won't be hidden
 
Upvote 0

Forum statistics

Threads
1,223,941
Messages
6,175,537
Members
452,652
Latest member
eduedu

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