PDA

View Full Version : [SOLVED:] Create different file



Tom Jones
06-21-2015, 02:10 PM
I have an excel file with 2 sheets (one visible, Sheet1, and a hidden sheet, Sheet2) and I like using VBA code, to create 3 files on the desktop:
- .PDF File to copy data from Sheet1 (range A1: G30)
- .xlsx File to copy data from Sheet1 (range A1: G30), but delete button that acts VBA code
- .xls file (in which there are no Sheet1 and in Sheet2, to delete columns A:K, also would like to remove VBA code)

Can you help me?
Thanks.

mancubus
06-22-2015, 06:15 AM
Sub vbax_52969_ExportRange_Pdf_xl51_xl56()

Dim DTPath As String

DTPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"

'1) pdf
Sheets("Sheet1").Range("A1:G30").ExportAsFixedFormat Type:=xlTypePDF, Filename:=DTPath & "MyPDF.pdf"

'2) xlsx
Sheets("Sheet1").Range("A1:G30").Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
.Sheets(1).Cells(1).PasteSpecial
.SaveAs DTPath & "MyXLSX.xlsx", 51
.Close False
End With

'3) xls
Sheets("Sheet1").UsedRange.Copy
Workbooks.Add (xlWBATWorksheet)
With ActiveWorkbook
.Sheets(1).Cells(1).PasteSpecial
.Sheets(1).Columns("A:K").Delete
.SaveAs DTPath & "MyXLS.xls", 56
.Close False
End With

End Sub

Tom Jones
06-22-2015, 07:22 AM
Thank you very much mancubus.

mancubus
06-23-2015, 04:29 AM
you are welcome. i'm glad it helped.

please mark the thread as "solved" from thread tools.