Microsoft 365 Export PDF VBA suivant critères

raf26

XLDnaute Occasionnel
Bonjour le forum,

J'ai besoin de votre aide pour exporter en PDF certaines zones d'un onglet suivant des critères.

Ces zones se situent sur le même onglet du fichier mais peuvent varier suivant des critères

si B4=1 alors zone à exporter D2:K12
si B16=1alors zone à exporter D2:K25
si B29=1 alors zone à exporter D2:K38

Le nom du fichier à exporter est à récupérer en cellule O2 et le fichier est à enregistrer dans le dossier mes documents.

J'utilise habituellement cette macro qui fonctionne super bien, mais là je bloque pour l'adapter


VB:
Sub Export_PDF()
Dim objShell, LeNom$, strPath$
Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("MyDocuments") & "\"
LeNom = Sheets("Donnees").[O2]
ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & LeNom & ".pdf"

MsgBox "Le fichier PDF est sauvegardé dans Mes Documents"
End Sub


Fichier test en pièce jointe.

Je vous remercie par avance et vous souhaite un bon week-end.
 

Pièces jointes

  • test_pdf.xlsx
    551.2 KB · Affichages: 4
Solution
Bonjour @raf26

Je te propose ceci :

VB:
Sub Export_PDF()
Dim objShell, LeNom$, strPath$, Cptr As Byte, ZoneImprim
Cptr = WorksheetFunction.Sum(Worksheets("Feuil1").Range("B4,B16,B29"))
Select Case Cptr
Case 1
    ZoneImprim = "$D$2:$K$12"
Case 2
    ZoneImprim = "$D$2:$K$25"
Case 3
    ZoneImprim = "$D$2:$K$38"
End Select

With ActiveSheet.PageSetup
    .PrintArea = ZoneImprim
    .Orientation = xlLandscape ' mode paysage ==> A choisir
    '.Orientation = xlPortrait  ' mode portrait ==> A choisir
End With

Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("MyDocuments") & "\"
LeNom = Sheets("Donnees").[O2]
ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & LeNom & ".pdf"

MsgBox "Le fichier PDF est...

Phil69970

XLDnaute Barbatruc
Bonjour @raf26

Je te propose ceci :

VB:
Sub Export_PDF()
Dim objShell, LeNom$, strPath$, Cptr As Byte, ZoneImprim
Cptr = WorksheetFunction.Sum(Worksheets("Feuil1").Range("B4,B16,B29"))
Select Case Cptr
Case 1
    ZoneImprim = "$D$2:$K$12"
Case 2
    ZoneImprim = "$D$2:$K$25"
Case 3
    ZoneImprim = "$D$2:$K$38"
End Select

With ActiveSheet.PageSetup
    .PrintArea = ZoneImprim
    .Orientation = xlLandscape ' mode paysage ==> A choisir
    '.Orientation = xlPortrait  ' mode portrait ==> A choisir
End With

Set objShell = CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("MyDocuments") & "\"
LeNom = Sheets("Donnees").[O2]
ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & LeNom & ".pdf"

MsgBox "Le fichier PDF est sauvegardé dans Mes Documents"
End Sub

Merci de ton retour
 

Discussions similaires

Statistiques des forums

Discussions
312 964
Messages
2 094 010
Membres
105 912
dernier inscrit
willou3869