循环中的条件语句

时间:2016-08-24 16:51:59

标签: vba conditional

我做错了什么?我想创建一个选项卡的pdf文件,该选项卡通过不同选项卡中的列A中的列表运行。同时,我希望它引用列CH中的值来查看它是否大于0以及它是否保存在特定文件夹中。如果没有,请保存在其他文件夹中。

非常感谢帮助,因为我找不到解决方案而且已经困住了几个小时。谢谢!

代码:

Sub Generate_PDF_Files()
Application.ScreenUpdating = False
Sheets("Table").Activate
Range("A7").Activate

Set r = Range("CH7:CH185")
With ActiveSheet
    For Each erange In .Range("CH7:CH185")
        If erange.Value > 0 Then
            Sheets("Att A").Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                "L:\Mike89\Violations\" & X & ".pdf", Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
         End If

         If erange.Value = 0 Then 
             Sheets("Att A").Select
             ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                 "L:\Mike89\No Violations\" & X & ".pdf", Quality:=xlQualityStandard, _
                 IncludeDocProperties:=True, IgnorePrintAreas:=False,OpenAfterPublish:=False
         End If

     Next

     Do Until ActiveCell.Value = "STOP"
         X = ActiveCell.Value
         Range("DLR_NUM") = "'" & X

         Sheets("Table").Activate
         ActiveCell.Offset(1, 0).Activate
     Loop

End with
End Sub

1 个答案:

答案 0 :(得分:0)

Sub Generate_PDF_Files()

    Dim c As Range, X, fName As String, shtAtt As Worksheet

    Set shtAtt = Sheets("Att A")

    For Each c In Sheets("Table").Range("CH7:CH185").Cells

        X = c.EntireRow.Cells(1).Value
        If X = "STOP" Then Exit For

        c.Parent.Range("DLR_NUM") = "'" & X

        fName = "L:\Mike89\" & IIf(c.Value = 0, "No Violations\", "Violations\") & X & ".pdf"

        shtAtt.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fName, _
                      Quality:=xlQualityStandard, IncludeDocProperties:=True, _
                      IgnorePrintAreas:=False, OpenAfterPublish:=False

    Next c

End Sub