Export specific sheets as New WBK Each

adeel1
3StarLounger
Posts: 264
Joined: 04 Oct 2017, 15:47

Export specific sheets as New WBK Each

Post by adeel1 »

Hello All

I have excel file save in 97-2003 format, there is several sheets in it I just want to export some of sheets as new wbk each.
Sheets are Month data, Contracts, Expires (which want to export)
New wbk names will be respectively, Dec Hold Data, Under Process, Deleted
Want to save C:\Users\Adeel1\Desktop\in mail\
If already, files exist with same name than auto replace the files!
My sheet has formulas please export as values.


Adeel

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Eport specific sheets as New WBK Each

Post by HansV »

Here is a macro:

Code: Select all

Sub ExportSheets()
    Const strPath = "C:\Users\Adeel1\Desktop\in mail\"
    Dim arrSheets
    Dim arrFiles
    Dim i As Long
    Dim wbkS As Workbook
    Dim wshS As Worksheet
    Dim wbkT As Workbook
    Dim wshT As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    arrSheets = Array("Month data", "Contracts", "Expires")
    arrFiles = Array("Dec Hold Data", "Under Process", "Deleted")
    Set wbkS = ActiveWorkbook
    For i = LBound(arrSheets) To UBound(arrSheets)
        Set wshS = wbkS.Worksheets(arrSheets(i))
        wshS.Copy
        Set wbkT = ActiveWorkbook
        Set wshT = wbkT.Worksheets(1)
        With wshT.UsedRange
            .Value = .Value
        End With
        wbkT.SaveAs Filename:=strPath & arrFiles(i) & ".xls", FileFormat:=xlExcel8
        wbkT.Close SaveChanges:=False
    Next i
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

adeel1
3StarLounger
Posts: 264
Joined: 04 Oct 2017, 15:47

Re: Eport specific sheets as New WBK Each

Post by adeel1 »

thank you soo much sir for your kind help. :fanfare: :clapping: :thankyou: :thankyou:

adeel1
3StarLounger
Posts: 264
Joined: 04 Oct 2017, 15:47

Re: Eport specific sheets as New WBK Each

Post by adeel1 »

sorry for in conveyance, i encountered one issue that when i change the name to "Activation F/M Dec'19" ,"Process F/M Dec'19" ,"Deleted F/M Dec'19" for new WBk's code didn't work when i debug
its bring me here

Code: Select all

wbkT.SaveAs Filename:=strPath & arrFiles(i) & ".xls", FileFormat:=xlExcel8

Adeel

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Eport specific sheets as New WBK Each

Post by HansV »

A file name cannot contain a / \ : ? * " < > or |
So change the names to "Activation FM Dec'19" or "Activation F_M Dec'19" etc.
Best wishes,
Hans

adeel1
3StarLounger
Posts: 264
Joined: 04 Oct 2017, 15:47

Re: Eport specific sheets as New WBK Each

Post by adeel1 »

thank you for this, its working now

i changed this line although its working, just want to confirm is it correct as i found this by google.

Code: Select all

& ".xlsx", FileFormat:=xlOpenXMLWorkbook
adeel

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Eport specific sheets as New WBK Each

Post by HansV »

That is correct if you want to save as a .xlsx workbook.
Best wishes,
Hans

adeel1
3StarLounger
Posts: 264
Joined: 04 Oct 2017, 15:47

Re: Eport specific sheets as New WBK Each

Post by adeel1 »

Big thanks for your time and help.

Adeel