Export Workbook

Hi Experts,

I need Experts help create a macro which is able to export this workbook (attached) into different location (D:\File Recovery) by removing “Data” sheet an also all the macros in this workbook. The copied file need to rename as per “Data” sheet cell A3 & C3, e.g. “TXT_1 Ver_01”. Hope Experts could help.  

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

As requested, here's the code:

Option Explicit
Sub exportWorkbook()
Dim fName As String
Dim wkb As Workbook
Dim wks As Worksheet
Dim mySht As Worksheet
Dim mySheets() As String
Dim i As Long
Dim newWkb As Workbook

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Set wkb = ThisWorkbook
    Set wks = wkb.Sheets("Data")
    fName = "D:\File Recovery\" & wks.Range("A3").Value & wks.Range("C3").Value
    'fName = ThisWorkbook.Path & "\" & wks.Range("A3").Value & wks.Range("C3").Value & ".xls"
    For Each mySht In ThisWorkbook.Worksheets
        If mySht.Name <> "Data" Then
            ReDim Preserve mySheets(i) As String
            mySheets(i) = mySht.Name
            i = i + 1
        End If
    Next mySht
    ReDim Preserve mySheets(UBound(mySheets) - 1)
    Call RemoveAllMacros(ActiveWorkbook) 'in case there are macros in the sheet's codepages
    ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlExcel8
    MsgBox "Successful export of " & fName
    Application.DisplayAlerts = False
    Application.ScreenUpdating = True
End Sub

Open in new window

I put a command button on the Data sheet to initiate the process.  Process, enumerates all sheets into an array mySheets() excluding the "Data" sheet.  Then, a COPY is performed which creates a new workbook.  A call to RemoveAllMacros() is called, in case there was any code in any sheets' codepage (source: http://www.exceltip.com/show_tip/Modules,_Class_Modules_in_VBA/Delete_all_macros_in_a_workbook/document_using_VBA_in_Microsoft_Excel/505.html).  Then that workbook is saved to the path and filename parameters you specified, and with displayalerts = false, an overwrite would occur if that file already existed.

See attached.


Oops.  Download this one, as I needed to add the & ".xls" to your filename :)

Billa7Author Commented:
Hi Dave,

It shows runtime error "1004", how to fix this?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

on what line did you get the runtime error?
Billa7Author Commented:

at this line "ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlExcel8"
Apologies - I had a typeo in the fName= line

here's the corrected file:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That was a bit embarrassing.  Have you got it working ok now?

Billa7Author Commented:
Hi Dave,

Works perfectly. Thanks a lot for the help.
Billa7Author Commented:
Hi dlmille,

Thanks a lot for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.