1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

    Yours,
    Chandoo
  2. Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  3. When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Use Save as to save one work sheet in a new work book

Discussion in 'VBA Macros' started by Petar Willhite, Oct 12, 2017.

  1. Petar Willhite

    Petar Willhite New Member

    Messages:
    9
    So I am trying to take the active sheet and save it to its own file when I run the following macro. Not sure why it is not running. I appreciate any help.

    Thanks,

    Petar

    Code (vb):
    Application.ScreenUpdating = False
    ActiveSheet.Range("$A$4:$AG$2001").AutoFilter Field:=4, Criteria1:= _
            "=01-Mechanical Engineer"
         
    With Worksheets("PC2 Billed IMPORT")
    Projectname = Cells(2, 2).Value
    End With


    ActiveSheet.SaveCopyAs ("\\C:\Users\petar.willhite\Desktop\DisciplineForecastFiles\" & "FC ME - " & Projectname & ".xlsm"), _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False



    ActiveSheet.Range("$A$4:$AG$2001").AutoFilter Field:=4
    Application.ScreenUpdating = True
    Last edited by a moderator: Oct 12, 2017
  2. Petar Willhite

    Petar Willhite New Member

    Messages:
    9
    Never mind I was able to figure it out and get it to work with this code

    'Mechanical Engineering File
    Code (vb):

    'Turning off Prompts and Stopping Screen Updating
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    'Remove Selected Formulas
    Columns("A").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False


    'Going to Remove Extra Sheets
    Worksheets("PM Tool").Delete


    'Filtering to Specific Discipline
    ActiveSheet.Range("$A$4:$AG$2001").AutoFilter Field:=4, Criteria1:= _
            "=01-Mechanical Engineer"
    'Getting The Project Name
    Projectname = Worksheets("PC2 Billed Import").Cells(2, 2).Value

    'Setting the file save path and name
    Path = "C:\Users\petar.willhite\Desktop\DisciplineForecastFiles\"
    Filename = "FC Mechanical Engineering - " & Projectname

    'Saving the new file to the folder with name above
    ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xlsm", _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="", WriteResPassword:="", _
    ReadOnlyRecommended:=False, CreateBackup:=False

    'Removing Filter
    ActiveSheet.Range("$A$4:$AG$2001").AutoFilter Field:=4

    'Turning Prompts and Screen Updating Back on
    Application.ScreenUpdating = True
    Application.DisplayAlerts = False
    Last edited by a moderator: Oct 12, 2017
  3. Hui

    Hui Excel Ninja Staff Member

    Messages:
    10,836
    Petar

    I don't think Saveas saves the worksheet, but in fact saves the whole workbook as a new file

    To save a Worksheet, you Copy it to a new Workbook and then save that workbook as a new file

    Code (vb):

    Sub CopySaveWorksheetAsWorkbook()
    Dim wb As Workbook
    Set wb = Workbooks.Add
    ThisWorkbook.Sheets("Sheet1").Copy Before:=wb.Sheets(1)
    wb.SaveAs "C:\MyDir\NewFileName.xlsx"
    End Sub

Share This Page