Loop back to dialog box

rmwilson

Board Regular
Joined
Apr 18, 2007
Messages
146
Is there a way to loop back for another filename on vbNo using this.

If Dir(SaveFileName) = "" Then 'do nothing
Else
resp = MsgBox(prompt:="This file already exists -- Overwrite Existing File?", Buttons:=vbYesNo)
If resp = vbNo Then
'Loop ?
exit function
end if
end if
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try something like this:

Code:
Dim MyFile As String
MyFile = Dir(SaveFileName) 
Do While MyFile <> ""
    resp = MsgBox(prompt:="This file already exists -- Overwrite Existing File?", Buttons:=vbYesNo)
    If resp = vbNo Then
        MyFile = Dir()
    Else
        Exit Do
    End If
Loop
 
Upvote 0

rmwilson

Board Regular
Joined
Apr 18, 2007
Messages
146
Andrew,

Thanks for your reply. Doesn't seem to loop under these conditions

If Left(Application.OperatingSystem, 3) = "Win" Then
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"Formatted text (Space Delimited) (*.SCT), *.SCT", , "Slicer 3")
Else
SaveFileName = Application.GetSaveAsFilename(CurFile, _
"TEXT", , "Slicer 3")
End If

' Check to see if Cancel was clicked.
Dim MyFile As String
MyFile = Dir(SaveFileName)
Do While MyFile <> ""
resp = MsgBox(prompt:="This file already exists -- Overwrite Existing File?", Buttons:=vbYesNo)
If resp = vbNo Then
MyFile = Dir()
Else
Exit Do
End If
Loop
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Is this what you want?

Code:
    Dim MyFile As String
    Do
        If Left(Application.OperatingSystem, 3) = "Win" Then
            SaveFileName = Application.GetSaveAsFilename(CurFile, _
                "Formatted text (Space Delimited) (*.SCT), *.SCT", , "Slicer 3")
        Else
            SaveFileName = Application.GetSaveAsFilename(CurFile, _
                "TEXT", , "Slicer 3")
        End If
'       Check to see if Cancel was clicked.
        If SaveFileName = False Then Exit Sub
        MyFile = Dir(SaveFileName)
        If MyFile <> "" Then
            resp = MsgBox(prompt:="This file already exists -- Overwrite Existing File?", Buttons:=vbYesNo)
            If resp = vbYes Then
                Exit Do
            End If
        End If
    Loop
 
Upvote 0

rmwilson

Board Regular
Joined
Apr 18, 2007
Messages
146
ADVERTISEMENT
Thanks Andrew, but it seems to loop even with a new filename entered. Can this be resolved?

Thanks!

rmw
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Maybe:

Code:
    Dim MyFile As String
    Do
        If Left(Application.OperatingSystem, 3) = "Win" Then
            SaveFileName = Application.GetSaveAsFilename(CurFile, _
                "Formatted text (Space Delimited) (*.SCT), *.SCT", , "Slicer 3")
        Else
            SaveFileName = Application.GetSaveAsFilename(CurFile, _
                "TEXT", , "Slicer 3")
        End If
'       Check to see if Cancel was clicked.
        If SaveFileName = False Then Exit Sub
        MyFile = Dir(SaveFileName)
        If MyFile <> "" Then
            resp = MsgBox(prompt:="This file already exists -- Overwrite Existing File?", Buttons:=vbYesNo)
            If resp = vbYes Then
                Exit Do
            End If
        Else
            Exit Do
        End If
    Loop
 
Upvote 0

Forum statistics

Threads
1,195,683
Messages
6,011,138
Members
441,587
Latest member
kbsgiri09

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
Top