UK97
UK97

Reputation: 248

Save multiple sheets in a new workbook in a particular file format

I tried to read up a few related posts on the forum but wasnt able to make a code work or understand the syntax of a few functions.

I will try to describe what I want to be done in a crisp fashion:

I am using Excel 2013, in case that is relevant.

The post may seem crude but I have no choice but to seek help from you as I have been breaking my head over this for the last day and a half and without this the rest of my macro project will become a waste. Thank you in advance for and suggestion/advice/ help.

If any other details or clarification is required please do ask.

I have added my lines of code that I have made but doesnt seem to work properly.

Sub Macro6()
'
' Macro6 Macro
' Save as
''
Dim varResult As Variant
Sheets(Array("sheet1", "sheet2", "sheet3")).Copy
varResult = Application.GetSaveAsFilename(FileFilter:= _
             "Excel Files *.xls", FileFormat:=-57, Title:="Save File", _
            InitialFileName:=ActiveWorkbook.Path \ Textstring123.xls)
If varResult <> False Then
ActiveWorkbook.SaveAs Filename:=varResult, _
FileFormat:=xlWorkbookNormal

     Exit Sub
    End If

End Sub

Highlighted in yellow is the line where debugger gives runtime error

Upvotes: 2

Views: 10183

Answers (1)

R3uK
R3uK

Reputation: 14547

This will do the trick, I have an issue with the Filters so I added a bit of error handling!

Option Explicit

Sub Macro6()
'
' Macro6 Macro
' Save as
''
Dim tB As Excel.Workbook
Dim wB As Excel.Workbook
Dim ExportArray As Variant
Dim ShName As Variant
Dim ExportName As String

Dim varResult As Variant

Set tB = ThisWorkbook
ExportArray = Array("sheet1", "sheet2", "sheet3")

For Each ShName In ExportArray
    Debug.Print ShName
    tB.Sheets(ShName).Copy
    Set wB = ActiveWorkbook
    On Error Resume Next
        ExportName = Application.GetSaveAsFilename(tB.Path & "\Textstring123", "Excel Files *.xls", , "Save " & ShName)
        If Err.Number > 0 Then
            ExportName = Application.GetSaveAsFilename(tB.Path & "\Textstring123", , , "Save " & ShName)
        Else
            'No error, everything went well with filters
        End If
    On Error GoTo 0

    'String 8 and Boolean 11
    If VarType(ExportName) <> 8 Then
        Exit Sub
    Else
        wB.SaveAs Filename:=ExportName, FileFormat:=xlWorkbookNormal
    End If
    DoEvents
    wB.Close
Next ShName

End Sub

Upvotes: 1

Related Questions