• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Create and Name PDF|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Create and Name PDF|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosCreate and Name PDF
sp_PrintTopic sp_TopicIcon
Create and Name PDF
Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
1
November 6, 2018 - 9:13 am
sp_Permalink sp_Print

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.

sp_AnswersTopicSeeAnswer See Answer
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
November 6, 2018 - 10:19 am
sp_Permalink sp_Print

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

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
3
November 6, 2018 - 12:05 pm
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
November 6, 2018 - 1:23 pm
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
5
November 6, 2018 - 1:33 pm
sp_Permalink sp_Print

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

sp_AnswersTopicAnswer
Answers Post
Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
6
November 6, 2018 - 2:42 pm
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
7
November 6, 2018 - 9:53 pm
sp_Permalink sp_Print

No worries 🙂

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
8
November 12, 2018 - 3:57 am
sp_Permalink sp_Print

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

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
9
November 12, 2018 - 3:59 am
sp_Permalink sp_Print

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.

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
10
November 13, 2018 - 12:10 pm
sp_Permalink sp_Print

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 is\isn'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

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
11
November 14, 2018 - 1:33 pm
sp_Permalink sp_Print

Many thanks Phil!!

 

Bob

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
12
November 14, 2018 - 2:26 pm
sp_Permalink sp_Print

no worries 🙂

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
13
November 24, 2018 - 5:48 am
sp_Permalink sp_Print

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

Avatar
Bob Kaplan
Seattle, WA, USA
Member
Members
Level 0
Forum Posts: 17
Member Since:
July 29, 2013
sp_UserOfflineSmall Offline
14
November 25, 2018 - 4:49 am
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
15
November 25, 2018 - 8:45 am
sp_Permalink sp_Print

Good job Bob.  The joy of coding.  A single character in the wrong place can break the whole thing.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Aislinn Mellamphy, Lynnette Altomari, Sherry Fox, Roy Lutke, Dieneba NDIAYE, Tucker Oakley
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Sopi Yuniarti
sandra parker
LAFONSO HERNANDEZ
Hayden Hao
Angela chen
Sean Moore
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Forum Stats:
Groups: 3
Forums: 24
Topics: 6214
Posts: 27243

 

Member Stats:
Guest Posters: 49
Members: 31894
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.