Solved

Excel Macro to copy specific Tabs into new workbook

Posted on 2015-01-23
5
127 Views
Last Modified: 2015-01-26
Hi,

I need your help in order to copy 2 Tab into a new workbook and then prompt me to save as with a default name that i can modify just a llittle bit before i press save.

My Tabs' name are "Summary" and "Table"
My default file name is "HC - MMMM - YYYY"
0
Comment
Question by:mldaigle1
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40566967
This should do it for you...

Sub CopyToNewWorkBookAndSave()
    Sheets(Array("Summary", "tabs")).Copy 'Copy without any further arguments copies to a new workbook and makes it the activeworkbook
    'Prompt for save-as name
    saveName = Application.GetSaveAsFilename("HC " & Format(Now(), "mmmm") & "-" & Format(Now, "yyyy"))
    If saveName <> False Then
        ActiveWorkbook.SaveAs saveName
    End If
End Sub

Open in new window


Further details on the GetSaveAsFilename options here

Works on Mac & PC.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40567088
You should probably add this function
Function FileExist(FilePath As String) As Boolean
'Source: http://www.rondebruin
Dim TestStr As String

'Test File Path (ie "C:\Users\Chris\Desktop\Test\book1.xlsm")
  On Error Resume Next
    TestStr = Dir(FilePath)
  On Error GoTo 0

'Determine if File exists
  If TestStr = "" Then
    FileExist = False
  Else
    FileExist = True
  End If

End Function

Open in new window

and then modify Simon's code with something like this
Sub CopyToNewWorkBookAndSave()
    Sheets(Array("Summary", "tabs")).Copy 'Copy without any further arguments copies to a new workbook and makes it the activeworkbook
    'Prompt for save-as name
    saveName = Application.GetSaveAsFilename("HC " & Format(Now(), "mmmm") & "-" & Format(Now, "yyyy"))
    If saveName <> False Then
        If vbYes = MsgBox("The file already exists, do you want to overwrite it?", vbexclamtion + vbYesNo) Then
          add modify code here to do what you want
        'ActiveWorkbook.SaveAs saveName
        End If
    End If
End Sub

Open in new window

0
 

Author Comment

by:mldaigle1
ID: 40570343
Hi,

I did try both solution and they are working fine.  Thanks,

Would it be possible to add the code to close the temporary workbook after I save the file without asking if i want to save it?  That way, it will bring me back to the master file where i ran the macro?
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40570368
Yes, immediately under the line
ActiveWorkbook.SaveAs saveName

add line
activeworkbook.close savechanges:=false

Martin may have been having an off day, because although he sensibly suggested a check for an existing copy of the file and provided a function for it, he didn't then call the function from the sub.

Here's an expanded version of the routine, including the function:
Sub CopyToNewWorkBookAndSave()
Dim saveName As Variant
    Sheets(Array("Summary", "tabs")).Copy 'Copy without any further arguments copies to a new workbook and makes it the activeworkbook
    'Prompt for save-as name
ChooseName:
    saveName = Application.GetSaveAsFilename("HC " & Format(Now(), "mmmm") & "-" & Format(Now, "yyyy"), filefilter:="Excel workbooks (*.xls*), *.xls*")
    If saveName <> False Then
        If Not saveName Like ".xlsx" Then saveName = saveName & ".xlsx" Else
        
        If FileExist(CStr(saveName)) Then
            If MsgBox("The file already exists, do you want to overwrite it?", vbexclamtion + vbYesNo) <> vbYes Then
            GoTo ChooseName
            End If
        End If
        Application.DisplayAlerts = False 'turn off alerts to overwrite any existing copy without prompt.
        ActiveWorkbook.SaveAs saveName
        Application.DisplayAlerts = True
        ActiveWorkbook.Close savechanges:=False
    End If
End Sub

Function FileExist(FilePath As String) As Boolean
'Source: http://www.rondebruin
Dim TestStr As String

  On Error Resume Next
    TestStr = Dir(FilePath)
  On Error GoTo 0

'Determine if File exists
  If TestStr = "" Then
    FileExist = False
  Else
    FileExist = True
  End If

End Function

Open in new window

0
 

Author Closing Comment

by:mldaigle1
ID: 40570471
Thanks alot, this is wonderful!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now