Solved

Excel Macro to copy specific Tabs into new workbook

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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