簡體   English   中英

Excel 循環驗證列表並導出為工作簿

[英]Excel cycle through validation list and export as Workbook

我有

-工作表名稱“sheet2:

-G1中的數據驗證列表

查看是否有一種方法可以讓宏循環遍歷數據驗證列表並導出一個工作簿,其中包含該單元格中的文本名稱並循環遍歷,以便列表中的每個項目都導出為自己的工作簿。 也就是說,如果下拉菜單中有 100 行,那么我將以 100 個 excel 文件結尾,每個文件都以 100 個下拉選項單獨命名。

例如,這對我打印 pdf 很有用:

Public Sub Create_PDFs()

Dim destinationFolder As String
Dim dataValidationCell As Range, dataValidationListSource As Range, dvValueCell As Range

destinationFolder = "C:\Users\DELL 04\Desktop\Q-Book Activities\Experiment"     'Same folder as workbook containing this macro
'destinationFolder = "C:\path\to\folder\"  'Or specific folder

If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
     
'Cell containing data validation in-cell dropdown

Set dataValidationCell = Worksheets("sheet2").Range("G1")
 
'Source of data validation list

Set dataValidationListSource = Evaluate(dataValidationCell.Validation.Formula1)
 
'Create PDF for each data validation value

For Each dvValueCell In dataValidationListSource
    dataValidationCell.Value = dvValueCell.Value
    With dataValidationCell.Worksheet.Range("A1:I45")
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With
Next
    End Sub

如果您只使用相同的工作代碼並改為創建工作簿會怎么樣?

For Each dvValueCell In dataValidationListSource
    Workbooks.Add.SaveAs Filename:=destinationFolder & dvValueCell
    Workbooks(dvValueCell & ".xlsx").Close
Next

嘗試更換:

.ExportAsFixedFormat Type:=xlTypePDF, Filename:=destinationFolder & dvValueCell.Value & ".PDF", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

Worksheets("sheet2").Copy 'it creates a new workbook with Sheet2 content
ActiveWorkbook.saveAs Filename:=destinationFolder & dvValueCell.Value & ".xlsx", FileFormat:=xlWorkbookDefault 
ActiveWorkbook.Close False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM