Beim Speichern der PDFs enthält der Dateiname ungültige Sonderzeichen
Verfahren CleanFileName
& CleanUsedRange
entfernen\ / : * ? | < > " Backspace Tab LF CR
Option Explicit Public Function CleanFileName(ByVal fName As String) As String Dim b() As Byte, specialChars As Variant, i As Long b = "\/:*?|<>" & Chr(34) & Chr(8) & Chr(9) & Chr(10) & Chr(13) specialChars = Split(StrConv(b, vbUnicode), Chr(0)) fName = Trim$(fName) 'Trim, then remove \ / : * ? | < > " Backspace Tab LF CR For i = 0 To UBound(specialChars) fName = Replace(fName, specialChars(i), vbNullString) Next CleanFileName = fName End Function
Public Sub CleanUsedRange(ByRef ur As Range) Dim arr As Variant, r As Long, c As Long arr = ur.Formula For r = 1 To UBound(arr, 1) For c = 1 To UBound(arr, 2) arr(r, c) = CleanFileName(arr(r, c)) Next Next ur.Formula = arr End Sub
.
So verwenden Sie die Verfahren in Ihren Subs
Private Sub CommandButton2_Click() Dim ws As Worksheet, fPath As String, fName As String, dt As String Set ws = ThisWorkbook.Worksheets("Sheet1") fPath = "C:\Users\Documents\test\" dt = Format(Date, " - MM-DD-YYYY") CleanUsedRange ws.UsedRange fName = fPath & ws.Range("C10") & dt & " - Quatation" ws.Range("A1:I60").ExportAsFixedFormat Type:=xlTypePDF, FileName:=fName End Sub
Private Sub SaveReport() Const FILE_PATH_1 = "C:\Users\heal1\OneDrive\Documents\test\" Const FILE_PATH_2 = "C:\Users\Documents\test\" Dim ws1 As Worksheet, ws3 As Worksheet, fPath As String, dt As String Set ws1 = ThisWorkbook.Worksheets("Sheet1") Set ws3 = ThisWorkbook.Worksheets("Sheet3") dt = Format(Now, "yyyy-mm-dd") Dim cfn As String, fName As String, lr As Long CleanUsedRange ws1.UsedRange lr = ws3.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1 'Transfer data to sheet3 ws3.Cells(lr, 1) = ws1.Cells(11, "C") ws3.Cells(lr, 2) = ws1.Cells(17, "C") ws3.Cells(lr, 3) = ws1.Cells(28, "I") ws3.Cells(lr, 4) = Now 'or dt ws3.Hyperlinks.Add Anchor:=ws3.Cells(lr, 5), Address:=fName, TextToDisplay:=fName 'Create invoice in PDF format cfn = ws1.Range("C11") & "_" & ws1.Range("C17") fName = FILE_PATH_1 & cfn & dt & ".pdf" ws1.ExportAsFixedFormat Type:=xlTypePDF, FileName:=fName 'create invoice in XLSX format Application.DisplayAlerts = False fName = FILE_PATH_2 & cfn & "_" & dt & ".xlsx" ThisWorkbook.SaveAs fName, FileFormat:=51 'ActiveWorkbook.Close Application.DisplayAlerts = True End Sub
.
Fügen Sie beides CleanFileName
und CleanUsedRange
ein generisches Modul hinzu
Zum Beispiel Module1