Solved

Excel Macro to copy specific Tabs into new workbook

Posted on 2015-01-23
5
139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 47

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

739 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