excel 如何自动选择属于列表中每个项目的特定工作表并将其导出为pdf文件

roqulrg3  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(140)

我有以下问题要在Excel中解决。我绝对不是VBA的专业人士。但我喜欢用VBA来发现编程,并解决问题,使事情变得更容易。当然,会有一个更好的方法来设置事情,但这是我所拥有的。

我有什么我有一个Excel工作簿,有几个工作表。“数据”,“计算”,“头版”,“模板A”,“模板B”等。

  • 数据表中有大量关于测试组成员的个人数据。
  • 计算表进行一些计算需要创建个人结果。
  • Frontpage工作表包含一些个人数据,用于使报告更“专业”。
  • 模板A和模板B工作表是不同报表的“模板”。这些模板将用于创建每个成员的单独报表。

"我所做的一切"
1.在运行宏之前,我选择了需要从中创建报告的模板(工作表)。

  • 接下来,宏(Sub Make_Reports_sheets)在成员列表中循环并创建报表。对于每个报表,将使用每个成员的个人数据创建一个新工作表。因此,可以根据我选择的模板数量为每个成员创建一个或多个报表(工作表)。工作表命名为:成员1_报表A、成员1_报表B、成员1_报表C、成员2_报表A、成员2_报表B等。

1.我已经有一个宏来创建一个pdf文件。但是我仍然缺少一段代码来实现我的目标。
"我想要的"

  • 当创建个人成员报告时,我希望将其导出为PDF文件。每个PDF文件包含属于某个成员的报告。必须包括一个首页。
    我需要解决以下问题
  • 如何自动选择工作表后,我创建的报告。把一个首页在它的前面,然后导出这个选择到一个pdf格式。然后继续下一个成员,并再次这样做,等等...

有没有人能解决这个问题?非常感谢!最相关的代码和平在这里

Sub Create_Report_Sheets()
    
    Dim SelectSheet As Object
    Dim ws As Worksheet
    Dim NewSheet As Worksheet   
    
    Dim ListOfNames As Range 'List can contain more than 50 members
    Dim Cell As Range
    
    Set Select.Sheet = ActiveWindow.SelectedSheets
    Set ListOfNames = DataSheet.Range(A1:A & LastRow)
    ...
    For Each Cell In ListOfNames
     For Each ws In SelectSheet
      ws.Copy After:=Sheets(Sheets.Count)
      Set NewSheet = ActiveSheet
      With NewSheet
       .Name = Cell.Offset(0,1) & "ReportA"
       'Do some stuff with this newsheet
      End With
     Next ws
    Next cell
End Sub
    
Sub Make_PDF()
    
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim Frontpage As Worksheet
    Dim SelectSheet As Object
    Dim PathFile As String
    
    Set Frontpage = ThisWorkbook.Sheets("FrontPage")
    ...
    'How to automatically select the reports (sheets) that belongs to a member?
    'How to include a Frontpage as first page of the pdf file?
    ....
    With ws
     .Select
     .ExportAsFixedFormat _
     .Type:=xlTypePDF, _
     .FileName:=PathFile
    End With
    
End Sub

我是否必须对SheetArray和ReDim执行某些操作?我是否必须将这两段代码组合在一起,然后一次性处理它们?

wfveoks0

wfveoks01#

这里肯定需要一个数组,所以您的假设是正确的:)
我希望在从我的testSub改回你的testSub时没有犯任何错误:

Sub Create_Report_Sheets()
    
    Dim SelectSheet As Object
    Dim ws As Worksheet, DataSheet As Worksheet
    Dim NewSheet As Worksheet
    Dim LastRow As Range
    Dim ListOfNames As Range 'List can contain more than 50 members
    Dim ccell As Range 'try not to use names used by excel/VBA as variable names
    Dim arrSheets As Variant
    Dim shAmount As Long
    Dim pathFile As String
    
    Set DataSheet = ThisWorkbook.Sheets("DataSheet")
    LastRow = DataSheet.Range("A" & Rows.Count).End(xlUp).Row 'fill your LastRow variable before using it
    Set SelectSheet = ActiveWindow.SelectedSheets 'Select.Sheet would throw an error here
    Set ListOfNames = DataSheet.Range("A1:A" & LastRow) 'don't forget your " with "A1:A"
    For Each ccell In ListOfNames
        shAmount = 1
        ReDim arrSheets(shAmount To shAmount)
        arrSheets(shAmount) = "FrontPage" 'put frontpage as your first element of your array
        For Each ws In SelectSheet
            ws.Copy After:=Sheets(Sheets.Count)
            Set NewSheet = ActiveSheet
            With NewSheet
             .Name = ccell.Offset(0, 1) & ws.Name 'if the names are in the A-column, why the offset? Probably also should use ws.Name instead of "ReportA"
             shAmount = shAmount + 1
             ReDim Preserve arrSheets(1 To shAmount) 'make Array bigger
             arrSheets(shAmount) = .Name
             'Do some stuff with this newsheet
            End With
        Next ws
        ThisWorkbook.Sheets(arrSheets).Select
        pathFile = ThisWorkbook.Path & " " & ccell.Offset(0, 1).Value2 & " Reports - " & Format(Now, "DD.MM.YYYY") & ".pdf"
         'adjust pathFile where needed
'~~~~ This will save over existing files with same name
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
            pathFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, OpenAfterPublish:=False
        Application.DisplayAlerts = False
 '       For i = 2 To UBound(arrSheets) 'uncomment the for loop if you want to delete the sheets after using them for the pdf file
 '       Sheets(arrSheets(i)).Delete
 '       Next i
        Application.DisplayAlerts = True
    Next ccell
End Sub

如果您还有任何问题,请随时提出。另外:查看此帖子了解不同方法:https://stackoverflow.com/a/36107539/19353309

相关问题