I have the following problem to solve within Excel. I am absolutely not a pro with VBA. But I like it to discover programming with VBA and to solve the problem to make things easier. And surely there will be a better way to set things up, but this is what I have.
What I have I have an excel workbook with several sheets. "Data", "Calc", "Frontpage", "TemplateA", "TemplateB", etc....
- Data sheet has a lot of personal data about members of a test group.
- Calc sheet does some calculations needed to create personal outcomes.
- Frontpage sheet includes some personal data and is used to make a report more 'pro'.
- TemplateA and TemplateB sheets are 'templates' for different reports. These Templates will be used to create individual reports of each member.
What I have done
- Before I run the macro, I select the templates (Sheets) from which I need to create the reports.
- Next, the macro (Sub Make_Reports_sheets) loops through the list of the members and creates the reports. For every report a new sheet is created with the personal data of each member. So, for each member one or more reports (sheets) can be created according of the number of templates I have selected. The sheets are named: Member1_ReportA, Member1_ReportB, Member1_ReportC, Member2_ReportA, Member2_ReportB, etc.
- I already have a macro to create a pdf file. But I still mis a piece of code to achieve my goal.
What I want to achieve
- When the individual member reports are created I want to export them to a pdf file. Each pdf files contains the reports which belongs to a member. A front page must me included.
What I need to solve is the following
- How to automatically select the sheets after I created the reports. Put a Front page in front of it and then export this selection to a pdf. And then continue to the next member and do the same again, etc...
Has anybody a solution how to solve this? Thanks a lot! The most relevant peace of code is here
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
Do I have to do something with a SheetArray and ReDim? Do I have to combine my two piece of code together and handle it all at once?