Objective: Add "payperdate" to the end of my file name just before the ".pdf".
Payperdate is a variable I've created that represents the date of the pay period that I get via a message box question. I store that date on every worksheet in a cell that's far from my other data so it's not in "H6", but it is identified by the variable.
I've tried modifying the code below that that does not work. Worse, I start getting error messages that the PDF might not have been saved etc. Can you please tell me how I might accomplish this??
Here's the code from Phil's macro:
'Current month/year stored in H6 (this is a merged cell)
CurrentMonth = Mid(ActiveSheet.Range("H6").Value, InStr(1, ActiveSheet.Range("H6").Value, " ") + 1)
'Create new PDF file name including path and file extension
PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Name _
& "_" & CurrentMonth & ".pdf"
Here's what I tried instead that failed:
Payperdate = Cells(3, finalcol + 20)
PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Name _
& "_" & Payperdate & ".pdf"
'Create new PDF file name including path and file extension
PDFFile = DestFolder & Application.PathSeparator & ActiveSheet.Name _
& "_" & Payperdate & ".pdf"
Any help would be greatly appreciated.
Hi Bob,
Without seeing all your code I can't be sure, and I can't run it to test it and see exactly what error messages you are getting, always best to include the workbook if you can to help us 🙂 , but
1) Does finalcol have a value?
2) Does Cells(3, finalcol + 20) contain anything?
Try debugging your VBA with Debug.print
Before creating the PDFFile string try this
Debug.Print Payperdate
Debug.Print finalcol
Regards
Phil
Hi Phil,
Thanks very much for replying to my message. Please don't laugh at my very poor code, but here's the file.
I must admit that my approach to debugging is to F8 my way through the code, but in this case the yellow highlighting jumps past where I can see what the contents are for that variable. I've never tried debug.print. I'll have to check that out.
As before, thanks for ANY help. I greatly appreciate it.
Bob Kaplan
No worries Bob.
I don't find anything logically wrong with your code, it doesn't generate an error on me. However, the value for Payperdate is empty on every sheet. You said that you get this value from asking the user for input which is fine, I guess I'm just missing that?. The code still works even if Payperdate is empty. You just get PDF file names like Mileages_.pdf
But do you need to store this on every sheet? Why not just store it on just one sheet (called Data) and access it that way e.g.
Sheets("Data").Range("A1").Value
Which brings up the point that the macro uses the data on the ActiveSheet. Anywhere you see code like this
ActiveSheet.Range("H6").Value
it's accessing the data on the currently active sheet. Which isn't necessarily the one you want to be getting data from. When you run your macro, do you have the correct sheet active?
So, I haven't encountered an error. Maybe you should go through my post on debugging, and enter some breakpoints then step through the code to examine variables and what is happening.
Also, it took me a minute to figure out you had assigned CTRL+C to one of your macros. It's good practice not to re-assign predefined shortcuts for your macros.
Cheers
Phil
Got it.
When you prompt the user for input for the value of Payperdate, it's in the format xx/xx/xx
The / are causing the issues because / isn't a valid character in Windows filenames.
When you try to create the PDFFile it tries to create (e.g.) Mileages_11/06/18.pdf and this isn't allowed.
You should reformat the Payperdate value to not include /
Cheers
Phil
Got it. I never thought of that (of course; ugh). I'll re-look at it and rethink based on your suggestions. Not sure why I decided to put the date on every sheet....I like your idea much better.
As always, you and Mynda come to my rescue!! Thanks very much!
Regards,
Bob
No worries 🙂
OK Phil. I think/hope I'm almost there. But, I still have a problem or two.
As you saw, I have 4 sheets that I do not need or want to create PDFs for. I've seen various ways to code this such as this way (below), but I'm not sure where in your code to place it.
Also, I'm hitting an error on this line "Attachments.Add PDFFile" specifying that I have not stated a correct path or something like that. How do I address this please?
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Input" And _
ws.Name <> "Mileages" And _
ws.Name <> "Payrates" And _
ws.Name <> "Rates" Then
ws.PrintOut
'ws.PrintPreview
End If
Next ws
Sorry. I meant to mention that I had used this code before I decided to have my output be a PDF vs printing hard copies.
Hi Bob,
As we'll be looping through every worksheet, but only saving some as PDF's, we need to have a way to flag that a sheet isisn't for exporting as PDF.
I didn't want to hard code sheet names into the VBA as potentially this would need to be changed every time we added, removed or renamed a sheet,
I thought about having a value on each sheet that the macro could read e.g. A1 has the value "NoExport" (or whatever), but this would be too messy. The macro would need to read the same cell on every sheet and we can't guarantee that the same cell won't have some other data in it.
What I have done is to change the name of any sheet you don't want to export, so that it starts with an underscore. This also gives you a quick visual indication of what sheets will be exported, you don't have to go through each sheet checking a value in a cell.
Wrap the main part of our code in this loop
For Each ws In ThisWorkbook.Worksheets
If Left(ws.Name, 1) <> "_" Then
... our code ...
End If
Next ws
In the attached workbook I've changed a couple of sheet names for testing purposes, you'll need to rename the correct sheets yourself.
In the workbook CurrentMonth was getting a value from H6 on every sheet, but it was empty. So I've changed it to pick up the value of A1 on the Data sheet. You can change this to whatever you wish.
Cheers
Phil
Many thanks Phil!!
Bob
no worries 🙂
Me again,
I am truly sorry to bother you, but I've hit another stumbling block and just cannot figure out what's wrong. I've attached my file as well as a Word doc showing the error I'm getting. As for the message I'm getting, I do not have any of the PDFs open. So, I'm baffled as to what is going on here. When I save to a folder where the prior PDFs are, I get the query about overwriting, I click yes, and it works fine.
On another note, while I really appreciate you already having created the code for me to be able to do this, there are several lines that I just do not understand what they are doing. Is there any way, that I could get some kind of explanations for these lines? I would gladly pay for them.
As always, thanks very much!
Bob Kaplan
I figured it out!!! It was due to the "/" in my date entry.
I fixed that and now I have other issues that I hope I can figure out myself too. 🙂
Thanks anyways,
Bob Kaplan
Good job Bob. The joy of coding. A single character in the wrong place can break the whole thing.