AW: Mehrere Dateien erstellen
28.06.2022 13:23:50
UweD
Hallo
mir ist aufgefallen, dass Dateien dann mehrfach erzeugt werden. Z.B. 104
Das solltest du prüfen
Hier der geänderte Code
Option Explicit
Public TMP As Boolean
Sub Automatisch()
Dim Von As Variant, Bis As Variant, ZVon As Integer, ZBis As Integer
Dim TB1 As Worksheet, TB3 As Worksheet
Dim Zeile As Range, SpID As Integer, i As Integer
Set TB1 = Sheets("Importliste")
Set TB3 = Sheets("Formular")
SpID = 6 'ID in Spalte F
Set Zeile = TB3.Range("C8")
Von = InputBox("Automatisch drucken Von", "Eingabe", 100) 'Beispiel
Bis = InputBox("Automatisch drucken Bis", "Eingabe", 200)
If Not IsNumeric(Von) Or Not IsNumeric(Bis) Then
MsgBox "Fehler bei Eingabe"
Exit Sub
End If
TMP = True 'Bei automatisch keine Msgbox
ZVon = WorksheetFunction.CountIf(TB1.Columns(SpID), Format(Von, "0000"))
If ZVon > 0 Then
ZVon = WorksheetFunction.Match(Format(Von, "0000"), TB1.Columns(SpID), 0)
Else
MsgBox Von & ": nicht gefunden"
Exit Sub
End If
ZBis = WorksheetFunction.CountIf(TB1.Columns(SpID), Format(Bis, "0000"))
If ZBis > 0 Then
ZBis = WorksheetFunction.Match(Format(Bis, "0000"), TB1.Columns(SpID), 0)
Else
MsgBox Bis & ": nicht gefunden"
Exit Sub
End If
For i = ZVon To ZBis
Zeile = TB1.Cells(i, 1) ' Zeile aus A für Sverweis wird gesetzt
Call PDFerstellen
Call Blattspeichern
Next
TMP=False
End Sub
Sub PDFerstellen() ' Dateiname ist Datum aus "B49"
Dim strPath As String, pdfOpenAfterPublish As Boolean
strPath = Range("B51")
If Not TMP Then
' MsgBox ist eine optionale Komponente
' Rückfragen, ob Datei nach dem Erstellen geöffnet werden soll
If MsgBox("Soll die PDF-Datei nach dem Erstellen angezeigt werden?", vbYesNo, "PDF anzeigen? ") = vbYes Then pdfOpenAfterPublish = True
' Vorgaben zum speichern des Dokuments
Else
pdfOpenAfterPublish = False
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & Range("B49").Value & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=IIf(pdfOpenAfterPublish, True, False) '(in Kombination mit MsgBox)
'OpenAfterPublish = True (wenn keine MsgBox gewünscht ist)
End Sub
und
Sub Blattspeichern()
Application.ScreenUpdating = False
ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:=Range("B50") & Range("B49") & ".xls"
ActiveWorkbook.Close
End Sub
LG UweD