mac excel vba loop:来自list&然后以PDF格式导出

时间:2016-11-19 08:03:15

标签: excel vba excel-vba pdf

因此迷失了自己:我在一张表(学生列表)上有一个列表,其中有160个学号。想要将每个学生编号粘贴在单元格A1中的反馈表中,然后将其作为pdf导出到以学生编号作为文件名的文件中。 到目前为止...... 干杯 麦克

Sub Pdfexportmacro()

Dim rCell As Range
Dim rRng As Range
Dim SNum As Integer

'Student numbers in cells A7:A160, set to A7:A9 for testing
Sheets("studentlist").Activate
Set rRng = Range("A7:A9")

For Each rCell In rRng.Cells
     SNum = rCell.Value

     ' Write student number to cell A1 on Feedback sheet:
        Sheets("Feedback").Activate
        Range(“A1”).Activate
        ActiveCell.Value = SNum

       ' Export & save file as pdf using SNum as filename:
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "Macintosh HD:Users:Michael:Desktop:" & rCell.Value, Quality:= _
        xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

Next rCell

End Sub

1 个答案:

答案 0 :(得分:1)

我不是MAC用户,因此我可能会遗漏一些我在Windows操作系统中没有的限制,但您可能会遇到以下情况:

Option Explicit

Sub Pdfexportmacro()

    Dim rCell As Range, rRng As Range

    'Student numbers in cells A7:A160
    Set rRng = Worksheets("studentlist").Range("A7:A160") '<--| set your "students" range

    With Worksheets("Feedback") '<--| reference "Feedback" worksheet
        For Each rCell In rRng '<--| loop through "students" range
            .Range("A1").Value = rCell.Value '<--| write current student number to cell A1 on Feedback sheet

           ' Export & save file as pdf using SNum as filename:
            .ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
            "Macintosh HD:Users:Michael:Desktop:" & rCell.Value, Quality:= _
            xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
        Next rCell
    End With

End Sub