Salvare PDF con più copie



  • Salvare PDF con più copie
    di Giulio (utente non iscritto) data: 12/12/2013 23:12:19


    Ciao, ho Excel 2010.
    Ho un file Excel nominato “DOCUMENTI” composto da 3 fogli: FATTURA, CMR, QUIETANZA.
    Ho adattato una macro (che allego) che lanciandola mi salva in un file PDF il foglio fattura (range A1:M66), in un altro file PDF il cmr (range A1:L76) e in un altro la quietanza (range A1:I61), dentro a 3 cartelle da me assegnate, inoltre mi rinomina i file con un nome da me assegnato come potete vedere nella macro (testo preso dalla cella P4).
    Io vorrei sapere se potete aiutarmi a modificare questa macro o farne una nuova che mi salvi in un UNICO FILE PDF: 2 copie del foglio FATTURA (range A1:M66), 2 copie del foglio CMR (range A1:L76) e una sola copia del foglio QUIETANZA (range A1:I61). Il file PDF sarà composto quindi da 5 fogli.
    Che questo file sia messo in "C:UsersLucaDocumenti" e rinominato con il testo preso dalla cella P2 del Foglio FATTURA.
    Non so usare il linguaggio Visual Basic, mi limito ad adattare e cercare di comprendere le macro che riesco a trovare. Vi ringrazio.
     
    Sub Macro2() 
    Sheets("FATTURA").Select
    Sheets(1).Range("A1:M66").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:UsersLucaDocumentiFatture da stampare" & Range("P4").Value & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Sheets("CMR").Select
    Sheets(4).Range("A1:L76").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:UsersLucaDocumentiCMR da stampare" & Range("P4").Value & ".pdf", 
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Sheets("QUIETANZA").Select
    Sheets(3).Range("A1:I61").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:UsersLucaDocumentiQuietanze da stampare" & Range("P4").Value & ".pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End Sub
    



  • di patel data: 13/12/2013 08:35:22

    sarebbe molto più semplice se si potessero salvare i fogli interi senza selezionare un range specifico





  • di patel data: 13/12/2013 08:41:54

    prova così
     
    Sub exportToPdfallSheet1pdf() ' tutti i fogli in un unico documento
        Dim mySheets As Sheets
        Set mySheets = Worksheets(Array("FATTURA", "FATTURA","CMR","CMR",QUIETANZA))
        filenameSave = "C:UsersLucaDocumentiFatture da stampare" & Worksheets("FATTURA").Range("P2") & ".pdf"
        With ConsolidationSh(mySheets, "")
            .ExportAsFixedFormat Type:=xlTypePDF, FileName:=filenameSave, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            Application.DisplayAlerts = False
            .Delete
            Application.DisplayAlerts = True
        End With
    End Sub
    






  • di patel data: 13/12/2013 08:48:06

    ho dimenticato una sub
     
    Function ConsolidationSh(mySheets As Sheets) As Worksheet
        Dim tempSheet As Worksheet
        Dim StartNewSheetRow As Long, StartNewSheetCell As Range
        Dim i As Long
        With mySheets(1).Parent
            Set tempSheet = .Worksheets.Add(Before:=.Sheets(1))
        End With
        
        For i = 1 To mySheets.Count
            StartNewSheetRow = ReallyUsedRange(tempSheet).Rows.Count + 1
            Set StartNewSheetCell = tempSheet.Cells(StartNewSheetRow, 1)
            StartNewSheetRow = ReallyUsedRange(tempSheet).Rows.Count + 1
            ReallyUsedRange(mySheets(i)).Copy Destination:=tempSheet.Cells(StartNewSheetRow, 1)
            tempSheet.HPageBreaks.Add Before:=StartNewSheetCell
        Next i
        tempSheet.Rows(1).Delete
        Set ConsolidationSh = tempSheet
    End Function






  • di Giulio (utente non iscritto) data: 13/12/2013 12:41:23

    SCUSA la mia ignoranza nell'argomento, ma non so usare il visual basic, come devo scrivere la macro intera? dove devo aggiungere il secondo pezzo che hai scritto?

    grazie dell'aiuto






  • di Giulio (utente non iscritto) data: 13/12/2013 13:48:30

    Scrivendo cosi la macro mi da "errore di compilazione: variabile non definita" e mi evidenzia nella macro filenameSave =

    ????
     
    Option Explicit
    
    Sub exportToPdfallSheet1pdf()
        Dim mySheets As Sheets
        Set mySheets = Worksheets(Array("FATTURA", "FATTURA", "CMR", "CMR", "QUIETANZA"))
        filenameSave = "C:UsersLucaDocumentiFatture da stampare" & Worksheets("FATTURA").Range("P2") & ".pdf"
        With ConsolidationSh(mySheets, "")
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=filenameSave, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
            Application.DisplayAlerts = False
            .Delete
            Application.DisplayAlerts = True
        End With
    End Sub
    
    Function ConsolidationSh(mySheets As Sheets) As Worksheet
        Dim tempSheet As Worksheet
        Dim StartNewSheetRow As Long, StartNewSheetCell As Range
        Dim i As Long
        With mySheets(1).Parent
            Set tempSheet = .Worksheets.Add(Before:=.Sheets(1))
        End With
        
        For i = 1 To mySheets.Count
            StartNewSheetRow = ReallyUsedRange(tempSheet).Rows.Count + 1
            Set StartNewSheetCell = tempSheet.Cells(StartNewSheetRow, 1)
            StartNewSheetRow = ReallyUsedRange(tempSheet).Rows.Count + 1
            ReallyUsedRange(mySheets(i)).Copy Destination:=tempSheet.Cells(StartNewSheetRow, 1)
            tempSheet.HPageBreaks.Add Before:=StartNewSheetCell
        Next i
        tempSheet.Rows(1).Delete
        Set ConsolidationSh = tempSheet
    End Function



  • di patel data: 13/12/2013 17:30:29

    io non ho anteposto alle sub "Option Explicit" perché non ho dimensionato le variabili, quindi o lo togli oppure devi dimensionare le variabili, se non conosci il vba non prendere iniziative.





  • di Giulio (utente non iscritto) data: 13/12/2013 17:40:09

    Ho tolto "Option Explicit" ma quando la lancio mi da "Errore di compilazione: nemero errato di argomenti o assegnazione di proprietà non valida"

    Sai per caso come si può risolvere per favore?
    Grazie



  • di Giulio (utente non iscritto) data: 13/12/2013 17:41:49

    mi da questo errore evidenziando nella macro "ConsolidationSh"



  • di patel data: 13/12/2013 17:45:44

    correggi
    With ConsolidationSh(mySheets, "")
    in
    With ConsolidationSh(mySheets)





  • di Giulio (utente non iscritto) data: 13/12/2013 17:54:59

    Grazie di patel,
    ora però mi da "Errore di compilazione: Sub o Function non definita" evidenziandomi nella macro "ReallyUsedRange"



  • di patel data: 13/12/2013 21:06:35

    scusami, dimentico sempre qualcosa, aggiungi
     
    Function ReallyUsedRange(aSheet As Worksheet) As Range
        Dim lastRow As Long, lastColumn As Long
        Dim i As Long
        With aSheet
            lastRow = 0
            For i = 1 To .UsedRange.Columns.Count + .UsedRange.Column
                lastRow = Application.Max(.Cells(.Rows.Count, i).End(xlUp).Row, lastRow)
            Next i
            lastColumn = 0
            For i = 1 To lastRow
                lastColumn = WorksheetFunction.Max(.Cells(i, .Columns.Count).End(xlToLeft).Column, lastColumn)
            Next i
            Set ReallyUsedRange = .Range("A1").Resize(lastRow, lastColumn)
        End With
    End Function