I am trying to convert this code that instead of saving PDF copies it saves the individual sheets as Excel workbooks instead. I have tried changing the Export as fixed format to xlsm but it appears to have a run time error. Very new to this but any help would be appreciated.
Sub PDF()
Dim xWs As Worksheet
Application.ScreenUpdating = False
For Each xWs In ThisWorkbook.Worksheets
If xWs.Visible = True Then
If xWs.Name <> "HOME" And xWs.Name <> "DATA" Then
xWs.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "\PDF P&L\" & Range("G1").Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
End If
Next xWs
Application.ScreenUpdating = False
End Sub
The .ExportAsFixedFormat method doesn't support export to excel file formats as per the documentation
The action you're most likely looking for is .move. When not specified with where to move, this will create a new workbook with the moved sheet. You can then use workbooks(Workbooks.count) to access the latest created workbook. See example code below:
Dim wb As Workbook
ActiveSheet.Move
Set wb = Workbooks(Workbooks.Count)
wb.SaveAs Filename:="yournamehere", FileFormat:=xlOpenXMLWorkbookMacroEnabled 'etc...
Please note, when this is done to the last remaining or only sheet in the workbook, this will throw an error. For more info on the .move method, see the link. For file formats to use see here.
Also, when moving a sheet, all the VBA code on the worksheet will be pulled across, but the modules related to the workbook won't. So attempting to save it as anything but xlsm when it has any code on it will result in a prompt or error.
Related
I´m using a VBA program that, after filling all the Textboxes in a vba userform, saves the workbook as PDF and also a copy of that workbook (xlsm). Part of the program is that the values of certain Textboxes are added as Doc Properties. It works that those properties are added to the original workbook and to the PDF, but not to the copy of the workbook and I don´t know why. Below is the code responsible for adding Doc Properties and saving the documents.
I have already tried to add "IncludeDocProperties:=True" to the part where the copy is saved, but that does not work.
ActiveWorkbook.BuiltinDocumentProperties("Keywords") = Userform1.TextBox1.Value & " " & UserForm1.TextBox2.Value
Dim varResult As Variant
Dim ActBook As Workbook
varResult = Application.GetSaveAsFilename(FileFilter:= _
"XLSM (*.xlsm), *.xlsm", Title:="save file", _
InitialFileName:=Userform1.TextBox1.Value)
Worksheets("Example").Copy
With ActiveWorkbook
.SaveAs Filename:=varResult, FileFormat:=xlOpenXMLWorkbookMacroEnabled
.Close savechanges:=True
End With
'PDF EXPORT
ActiveSheet.ExportAsFixedFormat Filename:=varResult, Type:=xlTypePDF, OpenAfterPublish:=True, _
IncludeDocProperties:=True
I have VBA where the user fills out a template, then saves as .XLSM and PDF.
The .XLSM saves as the entire workbook, but the PDF is only 2 worksheets. Both files are named after a variable cell in the workbook and a file location is suggested, but can be changed by the user.
Everything works until the user is warned that they are overwriting an existing file. If they select "no" or "cancel," then they get an error. Ideally, I would like for the sub to just exit and neither the PDF or .XLSM is saved. I have tried On Error, but cannot get the whole thing to work. Other solutions seem to take away some functionality (variable file name, different sheets printing/saving, initial file location, etc.).
Below is my code if anyone can help:
Sub SaveToPDF2()
Dim strFilename As String
Dim rngRange As Range
Dim fileSave As FileDialog
Set fileSave = Application.FileDialog(msoFileDialogSaveAs)
'Considering Sheet1 to be where you need to pick file name
Set rngRange = Worksheets("template").Range("b3")
'Create File name with dateStamp
strFilename = rngRange.Value & ".process." & Format(Date, "mm.dd.yyyy")
With fileSave
' Your default save location here
.InitialFileName = "U:\221 Released Drawings\" & strFilename
If .Show = -1 Then
ActiveWorkbook.SaveAs filename:=strFilename & ".xlsm", FileFormat:=52
ThisWorkbook.Sheets(Array("process", "signoff")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:=strFilename _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Else: Exit Sub
End If
End With
End Sub
Try inserting the next code line, just before With fileSave:
If Dir(strFilename & ".xlsm") <> "" then Exit Sub
If such a file already exists, the code is exited on the above inserted line...
I am working on a project to automate payroll and have developed a couple of Macros already, but am having trouble in the last step. I am copying data from one workbook to another, so that the timecard sheet is attached to the invoice sheet. I am then automating creating PDFs and when I create the PDFs, they shrink to fit the larger page that I have attached. I have inserted a manual page break to create two separate pages in my PDF, but the PDF is still shrinking the first page and not fitting it to the full page. Is there a way to zoom before the page break, so that the first page fits the full PDF size?
I have tried manually changing the PDF formatting, changing the print preview etc.
Sub Excel_to_PDF()
Dim Path As String
Dim filename As String
Dim ws As Worksheet
Dim nm As String
For Each ws In Worksheets
If ws.Visible = xlSheetVisible Then
ws.Select
nm = ws.Name
ActiveSheet.Rows(44).PageBreak = xlPageBreakManual
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="C:\Users\rober\Desktop\Invoices\" & nm & "-" & ActiveSheet.Range("K6").Value & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
Next ws
Dim ZoomRng As Range
Set ZoomRng = Range("A1:M43")
ZoomRng.Select
ActiveWindow.Zoom = True
End Sub
I want the PDF to be split between two pages. The first page before line 44, and the second page after. That is not a problem. This Macro works for that. What I need is for the sheet before line 44 to be zoomed and not scaled to fit with the same dimensions as the second sheet. The first sheet goes from A1:L43, and the second sheet goes from A50:AC110. The first sheet gets shrunk because of this, and I want it zoomed in on.
Try this i think the zoom was in the wrong place.
Sub Excel_to_PDF()
Dim Path As String
Dim filename As String
Dim ws As Worksheet
Dim nm As String
For Each ws In Worksheets
If ws.Visible = xlSheetVisible Then
ws.Select
nm = ws.Name
Dim ZoomRng As Range
Set ZoomRng = Range("A1:M43")
ZoomRng.Select
ActiveWindow.Zoom = True
ActiveSheet.Rows(44).PageBreak = xlPageBreakManual
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="C:\Users\rober\Desktop\Invoices\" & nm & "-" & ActiveSheet.Range("K6").Value & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If
Next ws
End Sub
I wrote a basic macro (very new to VBA) that extracts two columns from a workbook and puts them into a new workbook which I name and save. This works fine, but when I run it it opens and saves one as intended (e.g. "Health Care Portfolio") and opens (but doesn't name/save) a second blank book (with the standard "Book #" name). An excerpt of my code is below, could someone point out why this is happening? Thank you
Sub CreateHealthcare()
Sheets("Health Care").Select
With Application
.SheetsInNewWorkbook = 1
.Workbooks.Add
With Workbooks.Add
Workbooks("TVL Portfolio Creator.xlsm").Sheets("Health Care").Range("D:D").Copy
.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
Workbooks("TVL Portfolio Creator.xlsm").Sheets("Health Care").Range("E:E").Copy
.Sheets(1).Range("B1").PasteSpecial Paste:=xlPasteValues
.Sheets(1).Name = "Health Care Portfolio"
End With
ActiveWorkbook.SaveAs Filename:="C:\Users\example\Health Care Portfolio" & Format(Now(), " DDMMMYY") _
, FileFormat:=xlCSV, CreateBackup:=False
End With
End Sub
It seems to me your intention is for only 1 new workbook to be created and saved.
Your code contains the workbooks.add statement twice.
So taking the first out, should solve your problem.
If that doesn't work, I would recommend declaring a Workbook object, and referencing that.
Also the line Sheets("Health Care").Select doesn't really do anything since your copy-statements include an explicit reference.
Your code would look something like this:
Sub CreateHealthcare()
Dim NewBook as Workbook
Application.SheetsInNewWorkbook = 1
Set NewBook = Workbooks.Add
With NewBook
Workbooks("TVL Portfolio Creator.xlsm").Sheets("Health Care").Range("D:D").Copy
.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
Workbooks("TVL Portfolio Creator.xlsm").Sheets("Health Care").Range("E:E").Copy
.Sheets(1).Range("B1").PasteSpecial Paste:=xlPasteValues
.Sheets(1).Name = "Health Care Portfolio"
End With
NewBook.SaveAs Filename:="C:\Users\example\Health Care Portfolio" & Format(Now(), " DDMMMYY") _
, FileFormat:=xlCSV, CreateBackup:=False
End With
End Sub
I am using an Excel Macro that detects two worksheets and writes them to CSV format in their current SharePoint directory. However, upon executing the macro, it proceeds to open the newly created files within the same workbook and gives me the following error:
Run-time error '1004':
Sorry, we couldn't find C:\ProgramFiles(x86)\Google\Chrome\Application...
Is it possible it was moved, renamed or deleted?
Can I perform the "Save As" without opening the new file and avoiding the given error?
To be clear, it performs the core function just fine, as the new CSV files are properly written to the Sharepoint folder, I simply want to avoid the error message.
Macro code is as below:
Sub Export()
'
' Export Macro
' Export Rules and Privileges to 'Rules.csv' and Privileges.csv'
'
' Keyboard Shortcut: Ctrl+Shift+E
'
Dim ws As Worksheet
Dim path As String
path = ActiveWorkbook.path & "\"
For Each ws In Worksheets
If ws.Name Like "Rules" Then
ws.Activate
ws.SaveAs Filename:=path & "Rules.csv", FileFormat:=xlCSV, CreateBackup:=True
End If
If ws.Name Like "Privileges" Then
ws.Activate
ws.SaveAs Filename:=path & "Privileges.csv", FileFormat:=xlCSV, CreateBackup:=True
End If
Next
Range("B9").Select
Application.Run "RulesWorkbook.xlsm!Export"
Range("B4").Select
End Sub
Thank you to FreeMan for the solution in getting rid of the error message. While I did not figure out how to prevent Excel from opening the newly generated programs, I was able to side-step that by closing the workbook upon macro execution. Updated code for the macro is below:
Sub Export()
'
' Export Macro
' Export SecurityRules and Privileges to 'Rules.csv' and 'Privileges.csv'
'
' Keyboard Shortcut: Ctrl+Shift+E
'
Dim ws As Worksheet
Dim path As String
path = ActiveWorkbook.path & "\"
For Each ws In Worksheets
If ws.Name Like "Rules" Then
ws.SaveAs Filename:=path & "Rules.csv", FileFormat:=xlCSV, CreateBackup:=True
End If
If ws.Name Like "Privileges" Then
ws.SaveAs Filename:=path & "Privileges.csv", FileFormat:=xlCSV, CreateBackup:=True
End If
Next
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub