passer la list de validation des données et effectuer la macro d'printing

J'ai une list de validation de données qui contient les noms des employés chaque mois, je passe à travers chacun d'eux et appuyez sur un button d'printing avec la macro suivante.

Sub PDFActiveSheet() Dim ws As Worksheet Dim strPath As Ssortingng Dim myFile As Variant Dim strFile As Ssortingng On Error GoTo errHandler Set ws = ActiveSheet 'enter name and select folder for file ' start in current workbook folder strFile = Cells.Range("B1") & " Period " & Cells.Range("J1") strFile = ThisWorkbook.Path & "\" & strFile myFile = Application.GetSaveAsFilename _ (InitialFileName:=strFile, _ FileFilter:="PDF Files (*.pdf), *.pdf", _ Title:="Select Folder and FileName to save") If myFile <> "False" Then ws.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=myFile, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False, _ From:=1, _ To:=2 End If exitHandler: Exit Sub errHandler: MsgBox "Could not create PDF file" Resume exitHandler End Sub 

Ceci imprime la feuille dans la voie où le classur est enregistré.

Ma list de validation des données est dans la cellule 'B1'. Est-ce que je peux utiliser VBA pour faire une boucle dans la list et l'imprimer pour moi? Je n'ai pas vraiment réussi à faire un brouillon car utiliser une list de validation de données dans vba est complètement nouveau pour moi.

 Sub Loop_Through_List() Dim Name As Variant 'Dim List As ListBox? For Each Name in List Call PDFActiveSheet Next 

Vous pouvez utiliser quelque chose comme ceci:

 Sub Loop_Through_List() Dim cell As Excel.Range Dim rgDV As Excel.Range Dim DV_Cell As Excel.Range Set DV_Cell = Range("B1") Set rgDV = Application.Range(Mid$(DV_Cell.Validation.Formula1, 2)) For Each cell In rgDV.Cells DV_Cell.Value = cell.Value Call PDFActiveSheet Next End Sub 

Modifier: code révisé basé sur les commentaires ci-dessous:

 Sub Loop_Through_List() Dim cell As Excel.Range Dim rgDV As Excel.Range Dim DV_Cell As Excel.Range Set DV_Cell = Range("B1") Set rgDV = Application.Range(Mid$(DV_Cell.Validation.Formula1, 2)) For Each cell In rgDV.Cells DV_Cell.Value = cell.Value Call PDFActiveSheet Next End Sub Sub PDFActiveSheet() Dim ws As Worksheet Dim myFile As Variant Dim strFile As Ssortingng Dim sFolder As Ssortingng On Error GoTo errHandler Set ws = ActiveSheet 'enter name and select folder for file ' start in current workbook folder strFile = ws.Range("B1").Value & " Period " & ws.Range("J1").Value sFolder = GetFolder() If sFolder = "" Then MsgBox "No folder selected. Code will terminate." Exit Sub End If myFile = sFolder & "\" & strFile ws.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=myFile, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False, _ From:=1, _ To:=2 exitHandler: Exit Sub errHandler: MsgBox "Could not create PDF file" Resume exitHandler End Sub Function GetFolder() As Ssortingng Dim dlg As FileDialog Set dlg = Application.FileDialog(msoFileDialogFolderPicker) dlg.InitialFileName = ThisWorkbook.Path & "\" dlg.Title = "Select folder to save PDFs" If dlg.Show = -1 Then GetFolder = dlg.SelectedItems(1) End If End Function