Private Sub CommandButton1_Click()
Dim str1, str2, str3, str5, str6, str7, str8 As String
Dim i, x As Long
Dim str4 As String
str5 = Sheet2.Cells(1, 2).Value
str6 = "d:\检验记录\"
str7 = str6 & str5
If Dir(str7, vbDirectory) <> "" Then
MsgBox "该装箱清单已存在"
Else
MsgBox "文件夹不存在!,系统将创建该装箱清单文件夹"
MkDir str7 '创建文件夹
End If
str4 = Sheet2.Cells(5, 2).Value
Select Case str4
Case "AIRC"
For x = 36 To 40
With Sheet1
If .Range("C" & x).Value = "" Then Exit Sub
.Range("V3").Value = .Range("C" & x).Value '出厂
str2 = .Range("V3").Value
str1 = str7 & "\" & "ARIC"
If Dir(str1, vbDirectory) = "" Then MkDir str1 '创建文件夹
str3 = str1 & "\" & str2 & ".pdf"
If Dir(str3) <> "" Then MsgBox "该产品记录已存在"
.Activate
.Range("A1:U33").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
str3, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End With
Next x
你是要实现当产品记录的PDF文件存在了,就不再导出PDF,继续导出下一个产品记录吗?
把IF改成一下代码就可以了。
If Dir(str3) <> "" ThenMsgBox "该产品记录已存在"
Else
.Activate
.Range("A1:U33").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
str3, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End If来自:求助得到的回答