Need to add to code: Close file

daniels012

Well-known Member
Joined
Jan 13, 2005
Messages
5,219
Here is the code I am using now:

Code:
Dim Path As String, Path2 As String, CurrPath As String
    Application.ScreenUpdating = False
    ActiveWorkbook.Save
    CurrPath = ActiveWorkbook.Path
    Path = "C:\Documents and Settings\Owner\My Documents\Completed Proposals\"
    Path2 = "C:\Documents and Settings\Owner\My Documents\Surface Systems\"
    On Error Resume Next
    ActiveWorkbook.SaveAs Filename:= _
                          Path & "Proposal" & _
                          Str(Application.Range("O3").Value), FileFormat:=xlNormal _
                        , Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
                          CreateBackup:=False
    On Error GoTo 0
    new_file = ActiveWorkbook.Name
    Range("F2").Select
    Workbooks.Open Filename:=Path2 & "Proposal for XL.xls"
    Workbooks(new_file).Save
    Select Case Workbooks(new_file).Sheets("FRONT").Range("C2").Value
    Case "MD"
        Workbooks(new_file).SaveAs Filename:= _
                          "\\MIKESRGATEWAY\MikesProposals\" & Str(Application.Range("c3").Value) & new_file, FileFormat:=xlNormal _
                        , Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
                          CreateBackup:=False
    Case "TD"
        Workbooks(new_file).SaveAs Filename:= _
                          "\\Tomsblackibm\shareddocs\TomsProposals\" & Str(Application.Range("c3").Value) & new_file, FileFormat:=xlNormal _
                        , Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
                          CreateBackup:=False
    End Select
    Workbooks(new_file).Close
    ChDir CurrPath
    Application.ScreenUpdating = True

End Sub

After the code chooses the case and saves the name it's not closing this new form?

The code works fine it just won't close after the change is made.

Thank You
Michael
 
I tried to change and delete things that are in red. I think I can eliminate WB2??? I am really trying to write code???? Hope it works. With the changes below I think it will work from any file I call the file from not just "Proposal for XL".


Sub stuff()

Dim strPath As String, strPath2 As String, CurrPath As String

Dim WB1 As Workbook
Dim WB2 As Workbook

'Application.ScreenUpdating = False

Set WB1 = ActiveWorkbook

'First thing, save my work
WB1.Save

CurrPath = WB1.Path

'ASSUMING THAT C6 and O3 are BOTH in WB1
'move this line HERE: only do this once, and concatenate in the Select..Case later
'doing thsi inside the Select..Case pulls values from WB2, which might cause errors...
strfilename = Range("C6").Value & Range("O3").Value & ".xls"

strPath = "C:\Documents and Settings\Owner\My Documents\Completed Proposals\"

strPath2 = "C:\Documents and Settings\Owner\My Documents\Surface Systems\"

On Error Resume Next

'I then want to save my file
WB1.SaveAs Filename:=strPath & strfilename, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

On Error GoTo 0


'Here is where i need to choose the computer for it to go to. As well as give the file a name that the salesman recognizes. C6 is customer name and O3 is the proposal number
Select Case WB1.Sheets("FRONT").Range("C2").Value
Case "MD"
strfilename = "\\MIKESRGATEWAY\MikesProposals\" & strfilename

Case "TD"
strfilename = "\\Tomsblackibm\TomsProposals\" & strfilename

End Select

'WB2.SaveCopyAs Filename:=strfilename

ChDir CurrPath

Application.ScreenUpdating = True

WB1.Close

End Sub
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Error 1004 on this line:
Set WB2 = Workbooks.Open(Filename:=strPath2 & "Proposal for XL.xls")

The file is already open.


Michael
 
Upvote 0
WOW!
Hatman that worked well.
1 thing
1) It asks if I would like to save the file Yes, No, Cancel. The one created as proposal and the number. Example: "Proposal 13473"
Is there a way to avoid this message? I am good if it automatically saves it.


I really wish I could do what you have just done. How do you get so proficient writing code.
Michael

Add in the following two lines, at the bginning an end of the routine:

Code:
 Application.displayalerts = false
..code..
application.displayalerts = true

As for how to gain skill at writing code? Write more code. Been programming since I was about 8 years old... once you know the basics of how logic structures are constructed, languages are easy to learn, provided you have some sort of Help Files, or a reference manual available.
 
Upvote 0
As for the rest of the dialog that you have head with Norie... I am not sure that I follow all of it. You describe saving a new file, and then you talk about modifying an existing file. Do you need this code to handle 2 separate scenarios? If so, what are the parameters for EACh scenario... this should be pretty straight-forward once defined, but I am not getting a clear sense of what you realy need.
 
Upvote 0
Thank you for the last little bit also.

Code:
Application.displayalerts = false 
..code.. 
application.displayalerts = true


8-) :-D :) :-D 8-) :-P :-P
Thank You,
Michael
 
Upvote 0
OK
If I open the file that was created to "Completed Proposals" directory, Then try to run this code, it won't work. It is trying to get the info from "Proposal for XL" especially if "Proposal for XL" is opened already.

Is that clearer?

Michael
 
Upvote 0
Here is what I have:

Sub Save_and_SaveSalesman()


Dim strPath As String, strPath2 As String, CurrPath As String

Dim WB1 As Workbook
Dim WB2 As Workbook

'Application.ScreenUpdating = False

Set WB1 = ActiveWorkbook

'First thing, save my work
WB1.Save

CurrPath = WB1.Path

'ASSUMING THAT C6 and O3 are BOTH in WB1
'move this line HERE: only do this once, and concatenate in the Select..Case later
'doing thsi inside the Select..Case pulls values from WB2, which might cause errors...
strfilename = Range("C6").Value & Range("O3").Value & ".xls"

strPath = "C:\Documents and Settings\Owner\My Documents\Completed Proposals\"

strPath2 = "C:\Documents and Settings\Owner\My Documents\Surface Systems\"

On Error Resume Next

'I then want to save my file as "Proposal" and the number in Cell O3
WB1.SaveAs Filename:=strPath & strfilename

On Error GoTo 0

'I call this workbook "new_file"
'as long as you use the WB1 object, you should not need to store the name... - PES
' new_file = wb1.Name

'you should never need to select anything... - PES
' Range("F2").Select

'I want to open "Proposal for XL" so I can make a 2nd copy to the salesmans computer
Set WB2 = Workbooks.Open(Filename:=strPath2 & "Proposal for XL.xls")

'I have to save the "new_file"
'WHY??? you did this above - PES
' Workbooks(new_file).Save

'Workbooks(new_file).Close
'Here is where i need to choose the computer for it to go to. As well as give the file a name that the salesman recognizes. C6 is customer name and O3 is the proposal number
'Select Case WB2.Sheets("FRONT").Range("C2").Value
Select Case WB1.Sheets("FRONT").Range("C2").Value
Case "MD"
strfilename = "\\MIKESRGATEWAY\MikesProposals\" & strfilename

Case "TD"
strfilename = "\\Tomsblackibm\TomsProposals\" & strfilename

Case "DJ"
strfilename = "\\DAVEJONES\DavesProposals\" & strfilename

End Select

WB1.SaveCopyAs Filename:=strfilename

ChDir CurrPath

Application.ScreenUpdating = True

WB1.Close
End Sub

If I try to edit one of the created proposals from the directory "completed Proposals" It gives an error 1004 and says it can't do it.

Michael
 
Upvote 0
OK! I wrote code based on what you gave me and it seems to work fine!! :)

Code:
Sub Update_Save_and_SaveSalesman()


Dim strPath As String, strPath2 As String, CurrPath As String
    
    Dim WB1 As Workbook
    Dim WB2 As Workbook
    
    'Application.ScreenUpdating = False
    
    Set WB1 = ActiveWorkbook
    
    'First thing, save my work
    WB1.Save
    
    CurrPath = WB1.Path
    

    Select Case WB1.Sheets("FRONT").Range("C2").Value
        Case "MD"
            strfilename = "\\MIKESRGATEWAY\MikesProposals\" & strfilename
            
        Case "TD"
            strfilename = "\\Tomsblackibm\TomsProposals\" & strfilename
            
        Case "DJ"
            strfilename = "\\DAVEJONES\DavesProposals\" & strfilename
            
    End Select
    
    WB1.Save Filename:=strfilename
    
    ChDir CurrPath
    
    Application.ScreenUpdating = True
    
    WB1.Close
End Sub

Thank You,
Michael :lol: :-P :-P
 
Upvote 0
Try this:

Code:
Sub Save_and_SaveSalesman()


    Dim strPath As String, strPath2 As String, CurrPath As String
    
    Dim wb1 As Workbook
    Dim WB2 As Workbook
    
    Dim flag As Boolean
    
    Dim prop As String
    
    prop = "Proposal for XL.xls"
    
    Application.ScreenUpdating = False
    
    Set wb1 = ActiveWorkbook
    
    'First thing, save my work
    wb1.Save
    
    CurrPath = wb1.Path
    
    'ASSUMING THAT C6 and O3 are BOTH in WB1
    'move this line HERE: only do this once, and concatenate in the Select..Case later
    'doing thsi inside the Select..Case pulls values from WB2, which might cause errors...
    strfilename = Range("C6").Value & Range("O3").Value & ".xls"
    
    strPath = "C:\Documents and Settings\Owner\My Documents\Completed Proposals\"
    
    strPath2 = "C:\Documents and Settings\Owner\My Documents\Surface Systems\"
    
    On Error Resume Next
    
    'I then want to save my file as "Proposal" and the number in Cell O3
    wb1.SaveAs Filename:=strPath & "Proposal" & Str(Application.Range("O3").Value) & ".xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
    
    On Error GoTo 0
    
    'Here is where i need to choose the computer for it to go to. As well as give the file a name that the salesman recognizes. C6 is customer name and O3 is the proposal number
    Select Case WB2.Sheets("FRONT").Range("C2").Value
        Case "MD"
            strfilename = "\\MIKESRGATEWAY\MikesProposals\" & strfilename
            
        Case "TD"
            strfilename = "\\Tomsblackibm\TomsProposals\" & strfilename
            
    End Select
    
    flag = False
    
    For Each Item In Workbooks
    
        If Item.Name = prop Then
        
            flag = True
            
            Exit For
            
        End If
        
    Next Item
    
    If flag Then
    
        wb1.SaveCopyAs Filename:=strfilename
        
    Else
    
        'I want to open "Proposal for XL" so I can make a 2nd copy to the salesmans computer
        Set WB2 = Workbooks.Open(Filename:=strPath2 & "Proposal for XL.xls")
        
        WB2.SaveCopyAs Filename:=strfilename
        
    End If
    
    ChDir CurrPath
    
    Application.ScreenUpdating = True
    
    wb1.Close

    
End Sub

It will perform the same as my previous code, EXCEPT if the file is already open, it will simply save the current file rather than trying to open it again...
 
Upvote 0
OK! I wrote code based on what you gave me and it seems to work fine!! :)

I think not... you forgot this:

Code:
strfilename = Range("C6").Value & Range("O3").Value & ".xls"
 
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,421
Members
452,514
Latest member
cjkelly15

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
Back
Top