AnswerBun.com

How to modify these VBA codes to save excel sheet in excel format (.xlsx)?

Super User Asked by ramji on December 6, 2020

I have added a button in excel sheet and now when I click that button it saves the sheet at specific path with specific name but in pdf format.

I want to save this sheet simply in excel format (.xlsx). so

Sub PDFActiveSheet2()

Dim ws As Worksheet
Dim strFile As String

On Error GoTo errHandler

strFile = "m:formats" & Range("H8")
Set ws = ActiveSheet

ws.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

MsgBox "file has been created."

exitHandler:
        Exit Sub
errHandler:
        MsgBox "Could not create the file"
        Resume exitHandler

End Sub

What do I need to change?

One Answer

Change your following code from

ws.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=strFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

To the following code

ActiveWorkbook.SaveAs Filename:="C:Users46506090DesktopBook1.xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Change the path (C:Users46506090DesktopBook1.xlsm) to what you would want and see if it works.

CreateBackup:=False  

This is optional

Edit:

The complete code

Option Explicit

Sub Button1_Click()
'Sub PDFActiveSheet2()

Dim ws As Worksheet
Dim strFile As String

On Error GoTo errHandler

strFile = "m:formats" & Range("H8")
Set ws = ActiveSheet

ActiveWorkbook.SaveAs Filename:="C:Users46506090DesktopBook1.xlsm", _
            FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'Comment: Replace "C:Users46506090DesktopBook1.xlsm" to your desired filename
MsgBox "file has been created."

exitHandler:
        Exit Sub
errHandler:
        MsgBox "Could not create the file"
        Resume exitHandler

End Sub

Answered by Prasanna on December 6, 2020

Add your own answers!

Related Questions

receiving email addressed to another email address

1  Asked on November 4, 2021 by amy-leishman

   

Delete Local Accounts with Powershell Script

4  Asked on November 4, 2021 by alessio89g

 

Inverse grep using a pattern file doesn’t show non-matches

1  Asked on November 4, 2021 by that-brazilian-guy

       

What are “temp” folders used for in Windows 10?

2  Asked on November 3, 2021 by kingofsponges

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir