我想我需要循环

时间:2016-09-14 02:34:04

标签: excel vba excel-vba

VBA非常新,我设法创建了将生成和保存/发送电子邮件pdf的脚本。到目前为止,我已经为工作表中的每个更改创建了一个单独的行,例如:

Sheets("AM Dashboard").Select
Range("B5").Select
ActiveCell.FormulaR1C1 = "Employee1"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Google Drive\Sales Reports\Employee1\Sales Dashboard_Employee1" & " " & Format(Date, "yyyymmdd") & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
Range("B5").Select
ActiveCell.FormulaR1C1 = "Employee2"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Google Drive\Sales Reports\Employee2\Sales Dashboard_Employee2" & " " & Format(Date, "yyyymmdd") & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False

我想要做的是让代码循环通过sheet2列A中的列表,直到单元格为空,并使用相应的值填充AM Dashboard工作表的B5。

例如,A1将包含Employee1,A2 = Employee2,A3 = Employee3等。

1 个答案:

答案 0 :(得分:0)

试试这个

Sheets("AM Dashboard").Select
Range("B5").Select

While (Sheets("Sheet2").Cells(i, 1) <> "")
    ActiveCell.FormulaR1C1 = Sheets("Sheet2").Cells(i, 1)
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\Google Drive\Sales Reports\Employee" & Sheets("Sheet2").Cells(i, 1) & "\Sales Dashboard_Employee" & Sheets("Sheet2").Cells(i, 1) & "" & " " & Format(Date, "yyyymmdd") & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
    i = i + 1
Wend