Can error 1004 be trapped????

  • Thread starter Thread starter Richard m
  • Start date Start date
R

Richard m

I am still having a problem with trapping error 1004. Again I have cod
saving the spreadsheet as SaveAs, but when I select no of cancel, I ge
the end debug error 1004.

I have posted this several days ago with code and pulling hair out.

Los
 
Hi Richard,

You can trap error with "On Error" method.
Please look into "On error resume next" in your VBE help.
 
Richard,

Try this modification to your code (below).

HTH,
Shockley



Sub Macro3()

Dim ans As String
Dim ans2 As String
Dim number

ans = "C:\" & Range("playsave").Value
ans2 = Range("playsave").Value & ".xls"

'There are better ways of checking whether a directory
exists!
On Error Resume Next
MkDir ans
On Error GoTo 0

ChDir ans

'Tests for existence of file in current directory:
sTest = Dir(ans & "\" & ans2)

'Saves the file if it doesn't already exist:
If sTest = "" Then
ActiveWorkbook.SaveAs FileName:=ans & "\" & ans2

'Replaces the file only if "yes" is selected from the
message box:
Else
bTest = MsgBox("A file named " & sTest & " already exists in this
location. " & _
"Do you wish to replace it?", 4)
If bTest = 6 Then
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=ans & "\" & ans2
Application.DisplayAlerts = True
End If
End If
End Sub
 
Hello shockley,

I will be trying this code a little bit latter in the day. A couple o
questions pops up after reading the code.

on error resume next -- Don't really understand how this works but
will I will be reading up on this and on error goto 0.

Found out after the post that the path I am looking for is c:\folder1
and then I will write to c:\folder1\playsave\*.xls. I will have t
create the playsave folder if it does not exist. If it is not on
drive then the path is d:\folder1.

Thanks for the help in code. I sure enjoy this forum.

Richard
 
Back
Top