我想我需要循环

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

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等

尝试这个

 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