
Excel シートにボタンを追加しました。そのボタンをクリックすると、シートが特定のパスに特定の名前で PDF 形式で保存されます。
このシートを単純にExcel形式(.xlsx)で保存したいので
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
何を変える必要がありますか?
答え1
次のコードを
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
次のコード
ActiveWorkbook.SaveAs Filename:="C:\Users\46506090\Desktop\Book1.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
パスを変更する(C:\Users\46506090\Desktop\Book1.xlsm) を希望どおりに変更して、機能するかどうかを確認してください。
CreateBackup:=False
これはオプションです
編集:
完全なコード
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:\Users\46506090\Desktop\Book1.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
'Comment: Replace "C:\Users\46506090\Desktop\Book1.xlsm" to your desired filename
MsgBox "file has been created."
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create the file"
Resume exitHandler
End Sub
答え2
他の回答の訂正:保存場所は1つだけである必要があります
Option Explicit
Sub Button1_Click()
'Sub PDFActiveSheet2()
Dim ws As Worksheet
Dim strFile As String
On Error GoTo errHandler
strFile = "C:\Users\yourName\Desktop\Book1.xlsm"
'Comment: Replace "C:\Users\yourName\Desktop\Book1.xlsm" to your desired filename
Set ws = ActiveSheet
ActiveWorkbook.SaveAs Filename:=strFile, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
MsgBox "file has been created."
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create the file"
Resume exitHandler
End Sub