• 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...

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

Macro to copy and save a worksheet to a new workbook

skmerrey

New Member
Hi Everyone,

I've found the code below online. It originally exported all the worksheets to their own individual workbooks and saved them in the same location as the current workbook.

I've adapted it to export only one sheet named "EH", it creates the new workbook with this single sheet but doesn't save it and gives me a runtime error '424' Object required. When U press Debug it highlights the row of text that I have underlined.

Can anyone see what I've done wrong?

Code:
Sub Test18()

Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ThisWorkbook.Sheets("EH").Copy
    Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & EH.Name & ".xls"
    Application.ActiveWorkbook.Close False

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

Thanks
Shaun
 
Last edited by a moderator:
Dear Shaun
I understand that you need to export and save Tab named EH from the existing sheet to defined location. if i am right, try below code
Code:
Sub Test18()

Dim xPath As String
Dim strFilename As String

strFilename = "EH"

xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ThisWorkbook.Sheets("EH").Copy
    Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & strFilename & ".xls"
    Application.ActiveWorkbook.Close False

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
.
 
Back
Top