Excel VBA-通过下拉列表/验证列表进行迭代,并将生成的工作表保存到一个PDF

Excel VBA-通过下拉列表/验证列表进行迭代,并将生成的工作表保存到一个PDF,excel,vba,Excel,Vba,我正在迭代给定单元格上的下拉列表/验证列表: Sub SpitValues() Dim dvCell As Range Dim inputRange As Range Dim c As Range Dim i As Long 'Which cell has data validation Set dvCell = Worksheets(3).Range("D4") 'Determine where validation comes from Set inputRange = Worksheet

我正在迭代给定单元格上的下拉列表/验证列表:

Sub SpitValues()
Dim dvCell As Range
Dim inputRange As Range
Dim c As Range
Dim i As Long

'Which cell has data validation
Set dvCell = Worksheets(3).Range("D4")
'Determine where validation comes from
Set inputRange = Worksheets(2).Range("C4:C5")

i = 1
'Begin our loop
Application.ScreenUpdating = False
For Each c In inputRange
    dvCell = c.Value

    i = i + 1
Next c
Application.ScreenUpdating = True

End Sub  
在每次迭代中,我需要将整个工作表(3)保存到变量中,最后我需要将所有保存的工作表保存到一个PDF中,其中每个迭代数据都将位于单独的页面上。例如,如果我在下拉列表/验证列表中有五个项目,那么将有五页的PDF。可能吗

可能吗

对。这是可能的

首先,进行一些清理。我已经删除了
I
,因为您不使用该变量。我没有关闭屏幕更新,因为您希望提取每个迭代。但是,是的,关闭屏幕更新通常是一个很好的性能指标

执行此操作的一般算法是:

Identify where you are going to store the new pages
Make the change
Copy the page to the new store
loop
print
你已经做了一些,现在是为了完成这项工作

Sub SpitValues()
    Dim dvCell As Range
    Dim inputRange As Range
    Dim c As Range
    Dim NewWorkbook as workbook

    'Which cell has data validation
    Set dvCell = ThisWorkbook.Worksheets(3).Range("D4")
    'Determine where validation comes from
    Set inputRange = ThisWorkbook.Worksheets(2).Range("C4:C5")
    Set NewWorkbook = Application.Workbooks.Add

    'Begin our loop
    For Each c In inputRange
        dvCell = c.Value
        ThisWorkbook.Worksheets(3).Copy Before:=NewWorkbook.Sheets(NewWorkbook.Sheets.Count)  ' should insert this before the blank sheet at the end.
    Next c
'After this loop, print/save the new workbook. Change the file name to something suitable
    NewWorkbook.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="Whatever", _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

End Sub  

免责声明:我还没有机会测试这段代码。

这非常好。我的努力是不必要的复杂。非常感谢你。