XL 2019 Imprimer les différentes feuilles excel si complétées

schum23

XLDnaute Nouveau
Bonjour tout le monde,

Je reviens vers vous car j'ai encore besoin de vous concernant du VBA.
Je vous explique :

J'aimerai créer un bouton type userform "PRINT TO PDF" qui serait présent sur la page "LAST PAGE" de mon fichier excel. J'aimerai que quand on clique sur celui ci, il y ait une action type Print to PDF ou Save to Pdf et j'aimerai que les pages suivantes soit imprimées :

- 1st Page
- CMD_SOLUTIONS --> mais imprimer que jusqu'à la dernière ligne complétée et que si le tableau est complété
- Hardware_Software --> Que si il y a un élément dans le tableau en dessous
- LAST page

Ca serait top si tout était dans un fichier PDF.

Pensez-vous que c'est possible?
Je vous joins le fichier en annexe.

Je vous remercie d'ores et déjà pour votre aide.
 

Pièces jointes

  • OFFER_CALCULATIONS_BOUTON_PRINT_PDF.xlsm
    782.7 KB · Affichages: 14

danielco

XLDnaute Accro
Bonjour,

Essaie :

VB:
Sub Impression()
  Dim Plage As Range
  Application.ScreenUpdating = False
  With Sheets("CMD_SOLUTIONS")
    Set Plage = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Resize(, 7)
    .PageSetup.PrintArea = Plage.Address
  End With
  Sheets(Array("1st PAGE", "CMD_SOLUTIONS", "HARDWARE_SOFTWARE", "LAST PAGE")).Select
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  Application.ScreenUpdating = True
End Sub

Daniel
 

job75

XLDnaute Barbatruc
Bonjour schum23, danielco,

Voyez le fichier joint et la macro du bouton "PRINT TO PDF" :
VB:
Sub PDF()
Dim w As Worksheet, tablo, ncol%, i&, j%
Application.ScreenUpdating = False
For Each w In Sheets(Array("1st PAGE", "CMD_SOLUTIONS", "HARDWARE_SOFTWARE", "LAST PAGE"))
    w.Select False 'sélection multiple
    tablo = w.UsedRange 'matrice, plus rapide
    If Not IsArray(tablo) Then tablo = w.UsedRange.Resize(2) 'au moins 2 cellules
    ncol = UBound(tablo, 2)
    For i = UBound(tablo) To 1 Step -1
        For j = 1 To ncol
            If CStr(tablo(i, j)) <> "" Then GoTo 1
    Next j, i
1   If i Then w.PageSetup.PrintArea = w.UsedRange.Resize(i).Address 'zone d'impression
Next w
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\Mon PDF.pdf" 'nom à adapter
Sheets("LAST PAGE").Select
End Sub
Les 4 feuilles sont étudiées une par une pour définir la zone d'impression.

A+
 

Pièces jointes

  • OFFER_CALCULATIONS_BOUTON_PRINT_PDF(1).xlsm
    786.5 KB · Affichages: 9

schum23

XLDnaute Nouveau
Bonjour Job75,

Merci beaucoup pour ta réponse!!
Cependant, dernière petite adaptation à faire s'il vous plait :
- Concernant la 1st page : il faudrait limiter la zone d'impression à la colonne "i"
- CMD.solutions : limiter la zone d'impression à la colonne G
- Hardware_software : limiter à la colonne 'i"
- Last page : limiter impression à colonne "i"

Est-ce que ca serait possible?

Merci beaucoup!!!!

Bien à vous,
 

job75

XLDnaute Barbatruc
Fichier (2) avec le code modifié :
VB:
Sub PDF()
Dim w As Worksheet, ncol%, tablo, i&, j%
Application.ScreenUpdating = False
For Each w In Sheets(Array("1st PAGE", "CMD_SOLUTIONS", "HARDWARE_SOFTWARE", "LAST PAGE"))
    w.Select False 'sélection multiple
    ncol = IIf(w.Name = "CMD_SOLUTIONS", 7, 9) 'adapter éventuellement selon les feuilles
    tablo = w.UsedRange.Resize(, ncol) 'matrice, plus rapide
    For i = UBound(tablo) To 1 Step -1
        For j = 1 To ncol
            If CStr(tablo(i, j)) <> "" Then GoTo 1
    Next j, i
1   If i Then w.PageSetup.PrintArea = w.UsedRange.Resize(i, ncol).Address 'zone d'impression
Next w
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\Mon PDF.pdf" 'nom à adapter
Sheets("LAST PAGE").Select
End Sub
 

Pièces jointes

  • OFFER_CALCULATIONS_BOUTON_PRINT_PDF(2).xlsm
    786.8 KB · Affichages: 6

job75

XLDnaute Barbatruc
Quand tu dis :"Adapter éventuellement selon les feuilles", tu vois ca comment?
Par exemple avec la fonction Choose :
VB:
Sub PDF()
Dim w As Worksheet, n%, ncol%, tablo, i&, j%
Application.ScreenUpdating = False
For Each w In Sheets(Array("1st PAGE", "CMD_SOLUTIONS", "HARDWARE_SOFTWARE", "LAST PAGE"))
    w.Select False 'sélection multiple
    n = n + 1
    ncol = Choose(n, 9, 7, 8, 9) 'nombre de colonnes à adapter
    tablo = w.UsedRange.Resize(, ncol) 'matrice, plus rapide
    For i = UBound(tablo) To 1 Step -1
        For j = 1 To ncol
            If CStr(tablo(i, j)) <> "" Then GoTo 1
    Next j, i
1   If i Then w.PageSetup.PrintArea = w.UsedRange.Resize(i, ncol).Address 'zone d'impression
Next w
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\Mon PDF.pdf" 'nom à adapter
Sheets("LAST PAGE").Select
End Sub
Edit : le UsedRange de la 3ème feuille "HARDWARE_SOFTWARE" commence en colonne B il faut donc ncol = 8 pour limiter à la colonne "i".
 
Dernière édition:

schum23

XLDnaute Nouveau
Rebonjour,

Dernière petite update si c'est possible.
Peut-on rajouter que via le bouton, la macro imprime juste les onglets sélectionnées au préalable.
Je vous explique pourquoi:
- des fois les offres ne comprennent pas l'onglet "CMD-solutions"
- des fois les offres ne comprennent pas l'onglet "Hardware-software"
- des fois les offres comprennent tous les onglets.

Merci beaucoup pour votre aide!
Cordialement,
 

job75

XLDnaute Barbatruc
Bonsoir schum23,

Touche Ctrl enfoncée cliquez sur les onglets des feuilles à imprimer (sélection groupée).

Touches Ctrl+Z pour créer les zones d'impression et le fichier PDF via cette macro :
VB:
Sub PDF()
'se lance par les touches Ctrl+Z
Dim a, b, F As Object, S As Object, w As Worksheet, i As Variant, ncol%, tablo, j%
a = Array("1st PAGE", "CMD_SOLUTIONS", "HARDWARE_SOFTWARE", "LAST PAGE")
b = Array(9, 7, 8, 9) 'nombre de colonnes à imprimer
Set F = ActiveSheet
Set S = ActiveWindow.SelectedSheets 'sélection groupée
F.Select 'dégroupe
If IsError(Application.Match(F.Name, a, 0)) Then MsgBox "Sélectionnez en premier l'une des 4 feuilles...": Exit Sub
Application.ScreenUpdating = False
For Each w In S
    i = Application.Match(w.Name, a, 0)
    If IsNumeric(i) Then
        w.Select False 'regroupe
        ncol = b(i - 1)
        tablo = w.UsedRange.Resize(, ncol) 'matrice, plus rapide
        For i = UBound(tablo) To 1 Step -1
            For j = 1 To ncol
                If CStr(tablo(i, j)) <> "" Then GoTo 1
        Next j, i
1       If i Then w.PageSetup.PrintArea = w.UsedRange.Resize(i, ncol).Address 'zone d'impression
    End If
Next w
ActiveSheet.ExportAsFixedFormat xlTypePDF, ThisWorkbook.Path & "\Mon PDF.pdf" 'nom à adapter
F.Select 'dégroupe
End Sub
Fichier (3).

A+
 

Pièces jointes

  • OFFER_CALCULATIONS_BOUTON_PRINT_PDF(3).xlsm
    785.8 KB · Affichages: 7

schum23

XLDnaute Nouveau
Bonjour,

Quand j'imprime en PDF via le raccourcis, tout fonctionne, cela me le met en format PDF.
Cependant, quand j'ouvre avec Adobe, les feuilles PDF n'ont pas la même taille.
Auriez-vous un moyen de les avoir toutes à la même taille?

Merci
 

Discussions similaires