View Full Version : Prompt to create and save a file mid-routine


Rabbitoh
06-21-2010, 07:08 PM
I need some VBA code that will allow me to do the following

1. I run some code that is preset to open a workbook named “myworkbook.xls”
2. If it exists then it opens
3. However if it does not exist, then it presents a response box saying “myworkbook.xls does not exist. Create it now?"
4. If no selected then the routine ends
5. If yes selected, then a file named template.xls is opened and it then auto-saves to the said filename myworkbook.xls

If step 5 cannot be made to save to the myworkbook.xls automatically then I will accept that I will need to do it manually.

ajetrumpet
06-22-2010, 07:22 AM
Sub routine()

Dim mypath As String
Dim temppath As String
Dim xlfile As Excel.Workbook

mypath = "PATH\myworkbook.xls"
temppath = "PATH\template.xls"

If Dir(mypath) > "" Then
Set xlfile = Workbooks.Open(mypath)
Set xlfile = Nothing
Me.Close
Exit Sub
Else
If MsgBox("Files does not exist" & vbCr & "Create it now?", vbYesNo) = vbNo Then
Exit Sub
Else
Set xlfile = Workbooks.Open(temppath)
xlfile.SaveAs Filename:=mypath, FileFormat:=xlExcel8
Me.Close
Exit Sub
End If
End If

End Sub

Rabbitoh
06-27-2010, 06:34 PM
Nope, can't get this one to work. Code crashes out at the first Me.Close. All I have changed are the paths. they now point to the respective files as follows:

mypath = "c:\home\My Files\1111.xls"
temppath = "c:\home\My Files\template.xls"

Any ideas please.

Rabbitoh
06-30-2010, 12:47 PM
Nope, can't get this one to work. Code crashes out at the first Me.Close. All I have changed are the paths. they now point to the respective files as follows:

mypath = "c:\home\My Files\1111.xls"
temppath = "c:\home\My Files\template.xls"

Any ideas please.

chergh
07-01-2010, 03:38 AM
have you tried removing me.close?

Rabbitoh
07-04-2010, 05:58 PM
Did that but still won't work.

Rabbitoh
07-04-2010, 07:55 PM
Got it working now. Me.Close also removed. Thanks