Forum Discussion

olopa67's avatar
olopa67
Brass Contributor
Jan 24, 2022
Solved

saving a file with a cell value error

Hi all,

i ve got this VBA which save my file using a cell reference as a name, the code seems to work ok, the only issue is, if, and when i decide to re-save excel will ask me if i want to overwrite the existing file. if i answer no the macro will stop and give me a run-time error `1004` can someone please help me to fix it?

thank you in advance

 

Option Explicit
Sub SvMe()
'Saves filename as value of A1

Dim newFile As String, fName As String
fName = Range("D5").Value
newFile = fName & " "
ChDir _
"C:\Users\Paolo.De Luca\Documents\lETS PLAY WITH MACRO"
ActiveWorkbook.SaveAs Filename:=newFile

End Sub

 

  • On Error Resume Next # This line helps macro to resume when there is an error. Use it after Sub line:
    Sub SvMe()
    On Error Resume Next
    'Saves filename as value of A1

7 Replies

  • NowshadAhmed's avatar
    NowshadAhmed
    Iron Contributor
    On Error Resume Next # This line helps macro to resume when there is an error. Use it after Sub line:
    Sub SvMe()
    On Error Resume Next
    'Saves filename as value of A1
    • olopa67's avatar
      olopa67
      Brass Contributor
      thank you problem solved thanks to your advice, can i take advantage of your expertise and ask you how can i have a DO YOU WANT TO SAVE YES\NO? kind of option ?
      • NowshadAhmed's avatar
        NowshadAhmed
        Iron Contributor
        In your case it would be:
        On Error Resume Next
        Dim Result As Integer
        Result = MsgBox("Do you want to Save?", vbQuestion + vbYesNo)
        If Result = vbYes Then
        #### Your code here
        End If


        See if this works for you

Resources