Solved

Excel Macro to copy specific Tabs into new workbook

Posted on 2015-01-23
5
131 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

832 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