Prompt to create and save a file mid-routine (1 Viewer)

Rabbitoh

Registered User.
Local time
Today, 22:00
Joined
Jul 17, 2006
Messages
34
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

Banned
Local time
Today, 05:00
Joined
Jun 22, 2007
Messages
5,638
PHP:
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

Registered User.
Local time
Today, 22:00
Joined
Jul 17, 2006
Messages
34
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

Registered User.
Local time
Today, 22:00
Joined
Jul 17, 2006
Messages
34
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

Registered User.
Local time
Today, 22:00
Joined
Jul 17, 2006
Messages
34
Got it working now. Me.Close also removed. Thanks
 

Users who are viewing this thread

Top Bottom