L33
Board Regular
- Joined
- Jul 2, 2008
- Messages
- 108
Hi,
I have a macro that selects a number of worksheets (actually chart sheets mostly) and then generates a PDF of those selected sheets (using the standard "Save As PDF or XPS" functionality in Excel 2007).
I had the macro as a Private Sub against a sheet (called "Slide Selection") instead of a standard module, activated from a button press:
(My sincere apologies are due here - a lot of this is not my own code and is taken from a forum somewhere (possibly this one) but I didn't make a note of exactly where, and, more to the point, who, I took it from. I now understand that this is very poor form and it won't happen again!)
The user is presented with a list of all the slides available to them with tick boxes that get translated to simple "Y" or "N", which this code then reads and selects each one in turn before finally saving as PDF.
However, I now have a need to call on this code from within another larger macro. The larger macro basically makes copies of the master Excel report file but cuts out various elements of the core data to make sub-reports. But I want to incorporate the creation of this custom PDF for each of the sub-reports - so the user selects the slides he wants, then hits the button to create the sub-reports, including the PDF element.
Firstly, I can't call the "Private Sub CommandButton2_Click", and I didn't expect to. Changing it to a normal "Sub" seems to have no adverse affect on the normal function of the command button, but I still have trouble calling it.
I created a new version of the macro that can reside in a normal module making certain edits:
So all I've done is make it return to the "Slide Selection" worksheet on each loop to get the next "Y" or "N" value as it goes down the list.
My problem now is probably more basic than the length of this post will suggest: how do I deselect the worksheet "Slide Selection" before invoking the code to save as PDF??
Unless there is some valuable lesson I can be taught here, and I'm going about it all in completely the wrong way?
Many thanks in advance for any assistance!
I have a macro that selects a number of worksheets (actually chart sheets mostly) and then generates a PDF of those selected sheets (using the standard "Save As PDF or XPS" functionality in Excel 2007).
I had the macro as a Private Sub against a sheet (called "Slide Selection") instead of a standard module, activated from a button press:
Code:
Private Sub CommandButton2_Click()
Dim blnReplace As Boolean
Dim lngRow As Long
Dim lastRow As Single
Dim PDFSaveLocation As String
Dim PDFSaveNameCorp As String
Dim PDFSaveNamePeriod As String
PDFSaveLocation = ActiveWorkbook.Path
PDFSaveNameCorp = Range("I1")
PDFSaveNamePeriod = Range("I2")
blnReplace = True
lastRow = ActiveSheet.Cells(Rows.Count, "a").End(xlUp).Row
For lngRow = 2 To lastRow
If Cells(lngRow, 2) = "Y" Then
Sheets(Cells(lngRow, 1).Value).Select blnReplace
blnReplace = False
End If
Next
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFSaveLocation & "\" & PDFSaveNameCorp & " - " & PDFSaveNamePeriod & " - Custom PDF output"
End Sub
(My sincere apologies are due here - a lot of this is not my own code and is taken from a forum somewhere (possibly this one) but I didn't make a note of exactly where, and, more to the point, who, I took it from. I now understand that this is very poor form and it won't happen again!)
The user is presented with a list of all the slides available to them with tick boxes that get translated to simple "Y" or "N", which this code then reads and selects each one in turn before finally saving as PDF.
However, I now have a need to call on this code from within another larger macro. The larger macro basically makes copies of the master Excel report file but cuts out various elements of the core data to make sub-reports. But I want to incorporate the creation of this custom PDF for each of the sub-reports - so the user selects the slides he wants, then hits the button to create the sub-reports, including the PDF element.
Firstly, I can't call the "Private Sub CommandButton2_Click", and I didn't expect to. Changing it to a normal "Sub" seems to have no adverse affect on the normal function of the command button, but I still have trouble calling it.
I created a new version of the macro that can reside in a normal module making certain edits:
Code:
For lngRow = 2 To lastRow
If Cells(lngRow, 2) = "Y" Then
Sheets(Cells(lngRow, 1).Value).Select blnReplace
blnReplace = False
End If
Sheets("Slide Selection").Select (False)
Sheets("Slide Selection").Activate
Next
So all I've done is make it return to the "Slide Selection" worksheet on each loop to get the next "Y" or "N" value as it goes down the list.
My problem now is probably more basic than the length of this post will suggest: how do I deselect the worksheet "Slide Selection" before invoking the code to save as PDF??
Unless there is some valuable lesson I can be taught here, and I'm going about it all in completely the wrong way?
Many thanks in advance for any assistance!