×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Copying an open excel document with VBA

Copying an open excel document with VBA

Copying an open excel document with VBA

(OP)
Hello,

I've written a procedure that checks the current year against a user-input year. If the two differ (i.e. the user selects next year), the workbook will be copied and saved with the current year appended to the file name.

However, I'm using the FileCopy statement and it returns an error if used on an open file. How would I copy the currently open excel file? I could use the activeworkbook.savecopyas statement, but this would change the location and name of the open file.

I would like to keep the original file open while I save the content under a different filename. Is this possible?

Here is the relevant code so far:

CODE

Public Sub EndOfYearCheck(NewPageName As Variant)

    Dim BaseDirectory As String
    Dim OpenFileName As String
    Dim OldFileName As String
    Dim NewFileName As String
    
    'Checking if a new year has passed
    If NewPageName(2) <> Year(Now) Then
'        If NewPageName(2) = "" Then 'use didn't enter anything
'            MsgBox "No year entered. Aborting...", vbCritical
'            End 'stop execution
'        End If
        MsgBox "New Year Detected. The previous year's data will now be saved and a new workbook will be created.", vbInformation
       
        '*******Enter Saving Information*******
        BaseDirectory = "H:\Test Folder\"
        OpenFileName = ActiveWorkbook.Name
        OldFileName = Mid(ActiveWorkbook.Name, 1, Len(ActiveWorkbook.Name) - 4) 'I want to return just the name of the open workbook, not the .xls file extension
        'MsgBox FileName
        
        NewFileName = BaseDirectory & OldFileName & " - " & (Year(Now) - 1) & ".xls"
        FileCopy "E:\QC Stuff\" & OpenFileName, NewFileName
    End If

End Sub
Replies continue below

Recommended for you

RE: Copying an open excel document with VBA

Quote:


I could use the activeworkbook.savecopyas statement, but this would change the location and name of the open file.

I just tried this by modifying your code and the original file name stays open!  Not sure what you mean by location

CODE

ActiveWorkbook.SaveCopyAs NewFileName

ck1999

RE: Copying an open excel document with VBA

(OP)
Hey ck,

Hmm. I just used the savecopyas method and it worked perfectly. I'm not quite sure why I couldn't get it before.

Thanks for your help. winky smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close