Cancel Code if No or Cancel response

maxwell13

New Member
Joined
Jan 25, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi All

I am using the following code to save files into folders based on cell values.

VBA Code:
Sub makeDir()

Application.ScreenUpdating = False

    Dim str As String
    
    
    ActiveSheet.Copy
        Range("A1").Select
        
    str = "C:\" & Range("a2")
    
    If Len(Dir(str, vbDirectory)) = 0 Then
        MkDir str
    End If
    
    str = str & "\" & Range("a3")
    
    If Len(Dir(str, vbDirectory)) = 0 Then
        MkDir str
    End If
    
    str = str & "\" & Range("a4")
    
    If Len(Dir(str, vbDirectory)) = 0 Then
        MkDir str
    End If

    str = str & "\" & Range("a5")
    
    If Len(Dir(str, vbDirectory)) = 0 Then
        MkDir str
    End If
   
     ChDir str
  
ActiveWorkbook.SaveAs Filename:=str & "\" & Range("a7").Text, FileFormat:=51

   Range("a1").Select

    ActiveWorkbook.Save
    
    ActiveWorkbook.Close
    
    
   Application.ScreenUpdating = True
  
   
End Sub

If the file name exists this line gives a Yes No Cancel MsgBox to overwrite or not.


VBA Code:
ActiveWorkbook.SaveAs Filename:=str & "\" & Range("a7").Text, FileFormat:=51


Clicking Yes overwrites the file but No or Cancel throw an error and creates a new workbook.

Can I cancel the whole procedure if No or Cancel are selected so that a new workbook is not created and the user gets a MsgBox that nothing happened?

Thanks in advance for any help

maxwell13
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If the file name exists this line gives a Yes No Cancel MsgBox to overwrite or not.


VBA Code:
ActiveWorkbook.SaveAs Filename:=str & "\" & Range("a7").Text, FileFormat:=51


Clicking Yes overwrites the file but No or Cancel throw an error and creates a new workbook.

Can I cancel the whole procedure if No or Cancel are selected so that a new workbook is not created and the user gets a MsgBox that nothing happened?
I don't believe that line of code, by itself, will give a Yes No Cancel option.
 
Upvote 0
Ahh, I see what you are saying now.

Try replacing:

VBA Code:
ActiveWorkbook.SaveAs Filename:=Str & "\" & Range("a7").Text, FileFormat:=51

with the following:

VBA Code:
    FileNameToSave = Str & "\" & Range("a7").Text
'
    If Not Dir(FileToSave) <> "" Then                                       ' File doesn't exist yet
        ActiveWorkbook.SaveAs Filename:=FileNameToSave, FileFormat:=51
    Else                                                                    ' File exists already
        MsgBox "File name already exists. No action will be taken."
    End If
 
Upvote 0
Oops. correction needed for my last post here.

VBA Code:
If Not Dir(FileToSave) <> "" Then

should be:

VBA Code:
If Not Dir(FileNameToSave) <> "" Then

Sorry about that.
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,299
Members
448,885
Latest member
LokiSonic

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