循环访问相关列表选项并打印输出

时间:2016-11-08 08:36:29

标签: vba list drop-down-menu dependencies

我有一个数据验证下拉列表,每月报告需要77种不同的选择。我是一个vba新手,但是我找到了一个代码,用于从选择单元格A1(又名"策略")中打印出文件夹中当前所选的选项。我想要做的是添加一个代码循环,自动打印所有不同的选择,以分隔pdfs到同一个文件夹。我目前的代码是:

Sub PDFActiveSheet()

Dim ws                    As Worksheet
Dim myFile                As Variant
Dim strFile               As String
Dim sFolder               As String
On Error GoTo errHandler

Set ws = ActiveSheet

'enter name and select folder for file
' start in current workbook folder
strFile = ws.Range("A1").Value & " Strategy Output " & 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 String
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

我在本网站的前几页中找到的用于循环列表的代码如下所示,但我一直在运行时遇到1004错误(应用程序定义)。有谁知道为什么?

Private Sub Loop_Through_List()

Dim cell                  As Excel.Range
Dim rgDV                  As Excel.Range
Dim DV_Cell               As Excel.Range

Set DV_Cell = Range("Strategy")

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

0 个答案:

没有答案