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.  



Export.xls
Billa7Asked:
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.

dlmilleCommented:
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)
    
    ThisWorkbook.Sheets(mySheets).Copy
    Call RemoveAllMacros(ActiveWorkbook) 'in case there are macros in the sheet's codepages
    ActiveWorkbook.SaveAs Filename:=fName, FileFormat:=xlExcel8
    ActiveWorkbook.Close
    
    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.

Enjoy!

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

Dave
Export.xls
0
Billa7Author Commented:
Hi Dave,

It shows runtime error "1004", how to fix this?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

dlmilleCommented:
on what line did you get the runtime error?
0
Billa7Author Commented:
Hi,

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

here's the corrected file:
Export.xls
0

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
dlmilleCommented:
That was a bit embarrassing.  Have you got it working ok now?

Dave
0
Billa7Author Commented:
Hi Dave,

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

Thanks a lot for the help.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.