How can I remove ws.name - excel

I tried to create a PDF file using following script, it works but when I remove ws.Name, the script breaks.
Sub createPDFfiles()
Dim ws As Worksheet
Dim strName As String
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next 'Continue if an error occurs
' Name PDF files based on the worksheet Index (e.g Annex 1.1.1, Annex 1.1.2, etc.)
strName = Range("A10").Text & " " & Range("C7").Text & ws.Name
' If you want to name the PDF files differently just change the Fname variable above to
' whatever you like. For example if you changed Fname to:
'
' Fname = "C:\myFolder\pdfs\" & ActiveWorkbook.Name & "-" & ws.Name
'
' The files would be stored in C:\myFolder\pdfs, and named using the
' spreadsheet file name and the worksheet name.
'
' WARNING: Using worksheet names may cause errors if the names contain characters that Windows
' does not accept in file names. See below for a list of characters that you need to avoid.
'
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=strName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next ws
End Sub
How can I remove this ws.Name and still work?

Your Range objects are unqualified in this statement:
strName = Range("A10").Text & " " & Range("C7").Text & ws.Name
So they are referring to those cells on the active sheet, rather than on sheet ws.
If you wanted to refer to those cells on each worksheet you loop through, then qualify them appropriately:
strName = ws.Range("A10").Text & " " & ws.Range("C7").Text
You may wish to test that strName is not null before you try to save the PDF.
I'd also recommend losing the On Error Resume Next line, and handling errors properly.

Related

Trying to copy worksheets in multiple pdfs - Syntax Error

Could you kindly help me with this code? I am a beginner..
Sub ExportSheetstoPDF()
Dim ws As Worksheet
Dim mywsname As String
For Each ws In Worksheets
ws.Select
mywsname = ws.Name
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="Z:\Incent_2022\ORDINARIA\RETAIL-WHS\Schede Obiettivi\Q1\" & "Schede Obiettivi Retail Classico Q1 22_" & mywsname & “.pdf”, _
Next ws
End Sub
Your problem is that the line
Activesheet.ExportAsFixedFormat Type:=xlTypePDF, _
filename:="Z:\Incent_2022\ORDINARIA\RETAIL-WHS\Schede Obiettivi\Q1\" & "Schede Obiettivi Retail Classico Q1 22_" & mywsname & “.pdf”, _
has the continuation character _ at the end of it, but nothing continues on the next line.
Remove the ,_ from the end of the line
Also the “.pdf” needs to be ".pdf" i.e. using ordinary double-quotes and not ones copied out of an Office document ("intelligent" double-quotes)

Range export creates 50 individual PDFs - How to combine

I have an excel Dashboard document where cell D1 has a dropdown of 50 rep names. When D1 changes, all data on the page changes. My code exports an individual PDF for each value in D1 and loads it to the rep's personal file on our drive. I would like to also take all 50 of these PDFs and merge them into one single PDF file for our management team to review and save it in a seperate folder. My code currently looks like this:
Sub MakeFiles()
Dim rep As String
Dim reppath As String
Dim path As String
Dim pathmanagement As String
Dim MyFileName As String
Dim myrange As Range
Dim i As Range
On Error GoTo errHandler
ActiveWorkbook.Sheets("REF").Visible = False
ActiveWorkbook.Sheets("Individual").Activate
path = "C:\Users\ph\vf\Reporting\"
pathmanagement = "C:\Users\ph\vf\Reporting\management"
Set myrange = Worksheets("REF").Range("A2", Worksheets("REF").Range("a2").End(xlDown))
For Each i In myrange
Worksheets("Individual").Range("d1").Value = i
Application.Calculate
rep = Worksheets("Individual").Range("d1").Value
ActiveWorkbook.Sheets("Individual").Activate
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=path & ActiveSheet.Range("f1").Value & "\" & ActiveSheet.Range("g1").Value & "\" & "Territory Summary" & " " & ActiveSheet.Range("e1").Value & " " & Format(DateAdd("m", -1, Date), "mmmm yyyy") & ".pdf"
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pathmanagement & "\" & "Rep Territory Summaries" & "\" & "Territory Summary" & " " & ActiveSheet.Range("e1").Value & ".pdf"
Next i
MsgBox "Done!"
Exit Sub
errHandler: MsgBox "Could not create PDF file."
End Sub
Is there something I can add to this code to also get a single PDF that will show the results of all 50 values in D1? Or if I upload copies of each file into a separate folder, is there code that will then automatically merge them into one PDF file?
Export Multiple Versions of a Worksheet to PDF
Not tested.
The following should loop through column A of Source and write each value to D1 of Destination which will generate a different version of Destination due to formulas recalculating. Then this version will be exported as PDF to two paths (initially) and it will be copied to a newly added workbook (the addition). Finally, the new workbook will be exported as PDF and closed without saving changes.
Adjust AnotherFilePath appropriately.
Option Explicit
Sub MakeFiles()
Const RepPath As String = "C:\Users\ph\vf\Reporting\"
Const ManPath As String = "C:\Users\ph\vf\Reporting\management\"
On Error GoTo errHandler
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
Dim dws As Worksheet: Set dws = wb.Worksheets("Individual")
Dim sws As Worksheet: Set sws = wb.Worksheets("REF")
sws.Visible = False
' The following line assumes that the data doesn't contain any empty
' cells. Using `xlUp` is the preferred (usually safer) way.
Dim srg As Range: Set srg = sws.Range("A2", sws.Range("A2").End(xlDown))
Dim rwb As Workbook
Dim sCell As Range
Dim n As Long
For Each sCell In srg.Cells
dws.Range("D1").Value = sCell.Value
Application.Calculate
wb.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=RepPath & dws.Range("F1").Value & "\" _
& dws.Range("G1").Value & "\" & "Territory Summary" _
& " " & dws.Range("E1").Value & " " _
& Format(DateAdd("m", -1, Date), "mmmm yyyy") & ".pdf"
wb.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ManPath & "Rep Territory Summaries" & "\" _
& "Territory Summary" & " " & dws.Range("e1").Value & ".pdf"
n = n + 1
If n = 1 Then
dws.Copy ' adds a new workbook containing only the current 'dws'
Set rwb = ActiveWorkbook
Else
dws.Copy After:=rwb.Sheets(rwb.Sheets.Count)
End If
ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
Next sCell
rwb.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="AnotherFilePath" & ".pdf"
rwb.Close False
MsgBox "Exported " & n & " worksheets.", vbInformation, "PDF Export"
ProcExit:
Exit Sub
errHandler:
MsgBox "Could not create PDF file."
Resume ProcExit
End Sub

VBA Excel - Export PDF file from Excel with second page

Just want to ask if how can I export a PDF file with vba? The thing is I do have a 10-F and 10-B sheet. The code below is working in the 10-F sheet. My problem is how can I export the data in the 10-B sheet together with the 10-F? The first page is the data in 10-F while the data in 10-B will be on the second page.
The range for the 10-B sheet is "B10:AD92".
Sub Ver_PDF()
'Create and assign variables
Dim saveLocation As String
Dim rng As Range
lname = ThisWorkbook.Sheets("HOME").Range("K12")
fname = ThisWorkbook.Sheets("HOME").Range("K13")
Name = fname & " " & lname
pdfile = "V-" & Name & ".pdf"
saveLocation = ThisWorkbook.Path & "\V-PDF\" & pdfile
Set rng = Sheets("10-F").Range("B9:AD89")
Dim strFileExists As String
strFileExists = Dir(saveLocation)
If strFileExists <> "" Then
Dim Ret
'~~> Change this to the relevant file path and name
Ret = IsFileOpen(saveLocation)
If Ret = True Then
MsgBox "Please close the PDF file before proceeding...", vbCritical + vbOKOnly, "Error"
Exit Sub
End If
End If
rng.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=saveLocation, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Any help is highly appreciated! Thanks!
This code will do what you want. Change the worksheet names in the array as well as the destination path and file name.
Sub ExportAsPDF()
Dim FolderPath As String
Dim FileName As String
FolderPath = "D:\Test PDFs\" ' change to suit: end on back-slash
FileName = "Test" ' change to suit
On Error Resume Next
MkDir FolderPath
On Error GoTo 0
Worksheets(Array("10-F", "10-B")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=FolderPath & FileName & ".PDF", _
OpenAfterPublish:=True, _
IgnorePrintAreas:=False
MsgBox "PDF was successfully created."
Worksheets(1).Select
End Sub
Change OpenAfterPublish to False if you don't want to see the result right away.

How to insert multiple values in a single excel cell and export it as a pdf for every entry of that cell

i want to insert multiple values in a single cell and export every single entry as a separate pdf. The only thing i did till know is to manually reference the cells and export them as pdfs. This is my macro:
Sub SavePDF()
Range("A8").Value = Range("A8").Value + 1
Sheet3.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Users\Report_" & _
ActiveSheet.Range("A8").Value & ".pdf", _
OpenAfterPublish:=False
End Sub
Lets say that i have a range: M6:M14 and i want to input the results in the cell "M1". After i start the macro i want ot create for every single value (the value should be inside the pdf) a new pdf. Example: for the value of M6 a pdf, for M7 another and so on till i reach M14.
Please try this code.
Sub SavePDF()
Dim NameRange As Range
Dim i As Integer
Dim PdfName As String
Set NameRange = Range("M6:M14")
For i = 1 To NameRange.Cells.Count
PdfName = Trim(Range("A8").Value) & i
With Sheet3
.Range("M1").Value = NameRange.Cells(i).Value
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="C:\Users\Report_" & PdfName & ".pdf", _
OpenAfterPublish:=False
End With
Next i
End Sub

VBA exits before end

EDIT
I got it to run thru about 1,000 part numbers, but now it stops and excel freezes. Is it possibly too much for excel to handle? Would it be better to add a filter and have user perform this task based off criteria xyz?
I'm using the following code to create a mass form replacement. It's intended to run through a list of 5000+ part numbers, paste the part number into a designated range on another worksheet where it creates the form and saves it as a pdf in a specified folder. It works up until row 105 and then stops. It does what it's supposed to, other than continue down the sheet. I have the same code (modified slightly) being used on another sheet and it runs perfect. I'm not sure why its stopping after a certain number of rows
Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
Dim x As Worksheet
Dim y As Worksheet
Dim i As Long
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To FinalRow
Set x = ThisWorkbook.Worksheets("Part Number Database")
Set y = ThisWorkbook.Worksheets("BOM")
x.Cells(i, 1).Copy Destination:=y.Range("E3:J3")
strFile = Range("E3") & ".pdf"
strPathFile = strPath & strFile
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\AGoodwin\Desktop\BOM\" & strFile & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Next i
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End Sub

Resources