Excel 在下拉列表中选择特定字符串/数字时,仅将特定单元格导出为PDF

Excel 在下拉列表中选择特定字符串/数字时,仅将特定单元格导出为PDF,excel,vba,Excel,Vba,因此,当在下拉框中选择某个选项时,我尝试仅将某些单元格导出为PDF。我不确定在For循环中需要做什么。我总是得到一个空的PDF文件,当我试图导出它。我相信我已经设置了正确的if条件,但我不确定如何解决这个问题 Public Sub exportPDF() Dim wsA As Worksheet Dim wbA As Workbook Dim strTime As String Dim strName As String Dim strPath As String Dim strFile As

因此,当在下拉框中选择某个选项时,我尝试仅将某些单元格导出为PDF。我不确定在For循环中需要做什么。我总是得到一个空的PDF文件,当我试图导出它。我相信我已经设置了正确的if条件,但我不确定如何解决这个问题

Public Sub exportPDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim Answers As Double
Worksheets("Sheet1").Activate
On Error GoTo errHandler

'export to PDF if a folder was selected
 For Each Answer In Worksheets("Sheet1").Range("A2")
 If myFile <> "False" And Answer.Value = "99" Then
    ActiveWorkbook.Worksheets("Sheet1").Range("A3").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile

ElseIf myFile <> "False" And Answer.Value = "66" Then
ActiveWorkbook.Worksheets("Sheet1").Range("A2").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile

Else
ActiveWorkbook.Worksheets("Sheet1").Range("A1").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile
End if
Next Answer

End Sub
公共子导出PDF()
将wsA设置为工作表
将wbA设置为工作簿
作为字符串的Dim strTime
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
将strPathFile设置为字符串
Dim myFile作为变量
模糊的答案是双重的
工作表(“表1”)。激活
关于错误转到错误处理程序
'如果选择了文件夹,则导出为PDF
对于工作表(“表1”)中的每个答案。范围(“A2”)
如果myFile为“False”并回答.Value=“99”,则
ActiveWorkbook.Worksheets(“Sheet1”).Range(“A3”).ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
ElseIf myFile“False”并回答.Value=“66”则
ActiveWorkbook.Worksheets(“Sheet1”).Range(“A2”).ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
其他的
ActiveWorkbook.Worksheets(“Sheet1”).Range(“A1”).ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=myFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
'带有文件信息的确认消息
MsgBox“已创建PDF文件:”_
&vbCrLf_
&我的文件
如果结束
下一个答案
端接头

如果看不到数据,很难给出解决方案,但我已经尝试过了。不需要在单个单元格上循环,只需测试该单元格的必要值即可。另外,我将把
option explicit
放在顶部,这是为了避免使用未声明的变量,错误处理标签没有正确使用,因此没有必要保留它……您需要将标签放在底部,并提供一些错误处理代码,以使其正常工作。导出不太确定为什么要将特定范围
ActiveWorkbook.Worksheets(“Sheet1”).range(“A3”)
导出给我这不会导出整个页面,而只是其中的一部分

Option Explicit

Public Sub exportPDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

myFile = "exported file.pdf"

'export to PDF if a folder was selected
 If Range("A2").Value = 99 Then

    ActiveWorkbook.Worksheets("Sheet1").Range("A3").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile

ElseIf Range("A2").Value = 66 Then 
ActiveWorkbook.Worksheets("Sheet1").Range("A2").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile

Else
ActiveWorkbook.Worksheets("Sheet1").Range("A1").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile
End If
'Next Answer

End Sub

没有看到数据,很难给出解决方案,但我已经尝试过了。不需要在单个单元格上循环,只需测试该单元格的必要值即可。另外,我将把
option explicit
放在顶部,这是为了避免使用未声明的变量,错误处理标签没有正确使用,因此没有必要保留它……您需要将标签放在底部,并提供一些错误处理代码,以使其正常工作。导出不太确定为什么要将特定范围
ActiveWorkbook.Worksheets(“Sheet1”).range(“A3”)
导出给我这不会导出整个页面,而只是其中的一部分

Option Explicit

Public Sub exportPDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant

myFile = "exported file.pdf"

'export to PDF if a folder was selected
 If Range("A2").Value = 99 Then

    ActiveWorkbook.Worksheets("Sheet1").Range("A3").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile

ElseIf Range("A2").Value = 66 Then 
ActiveWorkbook.Worksheets("Sheet1").Range("A2").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile

Else
ActiveWorkbook.Worksheets("Sheet1").Range("A1").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=myFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
  & vbCrLf _
  & myFile
End If
'Next Answer

End Sub

似乎您只在一个单元格上运行循环。i、 e“范围(“A2”)”。您可以通过按F8(逐行执行)来运行代码…这将帮助您确定缺少的位置和内容。看起来您只在一个单元格上运行循环。i、 e“范围(“A2”)”。您可以通过按F8(逐行执行)来运行代码…这将帮助您识别缺失的位置和内容。