vba - message boxes and overwriting files

Ron512

Board Regular
Joined
Nov 17, 2002
Messages
98
I need help working through an issue with message boxes and overwriting files.

The below code is intended to check for an existing file. If a file exists then a yes/no message box opens. If yes is selected the file is overwritten, if no is selected a message box opens and the sub ends.

It works correctly if yes is selected but in no is selected it passes over the statements in the Else.

Code:
If Dir(WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name) <> "" Then
    If MsgBox("The file, " & save_name & " exists, do you want to overwite it?", vbYesNo + vbQuestion) = vbYes Then
    'overwrite the file
    wb.SaveAs Filename:=WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name, FileFormat:=51
    End If
Else
    'exit
    MsgBox "Ensure the data is currect." & vbCrLf & "The process will end.", vbOKOnly + vbExclamation
    Exit Sub
End If

Thanks

ron
 

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)
Ron,

Try this:

Code:
Sub msgTest()

If Dir(WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name) <> "" Then
    
    Ans = MsgBox("The file, " & save_name & " exists, do you want to overwite it?", vbYesNo + vbQuestion)
    
    Select Case Ans
        Case vbYes
            'overwrite the file
            wb.SaveAs Filename:=WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name, FileFormat:=51
        Case vbNo
            'exit
            MsgBox "Ensure the data is correct." & vbCrLf & "The process will end.", vbOKOnly + vbExclamation
            Exit Sub
        End Select
End If

End Sub
 
Upvote 0
Panda

Thanks for the quick reply. Your suggestion worked great. I added an Else so the file is saved if there was no existing file. Was that the correct way to handle it.

Code:
If Dir(WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name) <> "" Then
    
    Ans = MsgBox("The file, " & save_name & " exists, do you want to overwite it?", vbYesNo + vbQuestion)
    
    Select Case Ans
        Case vbYes
            'overwrite the file
            wb.SaveAs Filename:=WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name, FileFormat:=51
        Case vbNo
            'exit
            MsgBox "Ensure the data is correct." & vbCrLf & "The process will end.", vbOKOnly + vbExclamation
            Exit Sub
        End Select
Else
    wb.SaveAs Filename:=WAD_path & "\" & "WADs " & "Rev " & Rev & "\" & save_name, FileFormat:=51

End If

Ron
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,976
Members
448,934
Latest member
audette89

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