+ Reply to Thread
Results 1 to 7 of 7

Date torture

  1. #1
    Registered User
    Join Date
    10-10-2003
    Location
    Massachusetts
    MS-Off Ver
    Office 365
    Posts
    88

    Date torture

    We have been using the same code for years. Today 2 out of three computers started having a problem with the following code:

    Please Login or Register  to view this content.
    Todays date is 03/26/24. Cell A7 is formatted xx/xx/xx.


    Problem: On two computers, no matter what we do, including Custom format as xx/xx/xx the formula bar displays the date as x/xx/xx. On one computer the formula bar shows what we need to see: xx/xx/xx. This computer runs the code and saves the file.

    What the heck. The code has worked flawlessly for years. Why all of a sudden can't we force the formula bar to display xx/xx/xx on 2 of 3 computers?

    We use the same code to save all over the place. I looked at dates on the other tabs. Same deal. Format is x/xx/xx in formula bar. We tried saving from a different tab, and the save worked. We're stunned. One would think the code would fail everywhere the formula bar shows x/xx/xx, but that is not the case. Thankfully.

    Thanks to all for taking a look.

  2. #2
    Forum Contributor
    Join Date
    10-02-2012
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    252

    Re: Date torture

    The code itself is entirely for exporting to PDF and has no effect on the format of A7.

    I suspect it may be an settings issue;

    Do all computers have the matching setting regarding removing the first 0?

    Attachment 864196
    Attached Images Attached Images

  3. #3
    Registered User
    Join Date
    10-10-2003
    Location
    Massachusetts
    MS-Off Ver
    Office 365
    Posts
    88

    Re: Date torture

    We unchecked remove leading zeroes in data options. No help there.

    We do appreciate your taking the time to look.

  4. #4
    Forum Contributor
    Join Date
    10-02-2012
    Location
    UK
    MS-Off Ver
    Office 365
    Posts
    252

    Re: Date torture

    A very odd one that it would change like that.

    There are solutions to work around this though. Have you tried formatting A7 as plain text?

    It might also be a good idea to assign the value of A7 to a variable, strip the "/" using the replace function ie
    Please Login or Register  to view this content.
    and use the stripped variable "FileDate" in place of
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    10-10-2003
    Location
    Massachusetts
    MS-Off Ver
    Office 365
    Posts
    88

    Re: Date torture

    Well everyone, I did solve the problem.

    Agreed, very odd the problem should just pop u. I can tell you what I did.

    The sheet is populated from a form. txtDate is a field on the form.

    Here is what didn't work:
    Please Login or Register  to view this content.
    Here is what does work:
    Please Login or Register  to view this content.

    Now when we populate the sheet with txtDate without ".Text", the code runs as always. The formula bar displays xx/xx/xx.

    No clue why the code ran for years, then decided to bomb this AM on 2 of three desktops.

    In any event, we're off and running again.

    Many thanks to everyone that looked!!!!!

  6. #6
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,223

    Re: Date torture

    In my opinion, a mistake was made already at the conception stage of this project. If cell A7 is to store the date (as a number, not text), then care should be taken that the date is passed from the form, not the text. Note that TextBoxes (as well as several other controls) always store data in the form of text. When passing data from a control to a worksheet, force the appropriate data type (in this case, date).
    The general rule for passing data to and from a form:
    1. when passing from a worksheet to a form control, use the Format function, without a formatting parameter, e.g.
    Please Login or Register  to view this content.
    A short date should appear in the txtDate control according to Windows settings. You'll probably get 03/26/2024. Since I'm guessing you're using US date format, you could possibly pass the date to the control like this:
    Please Login or Register  to view this content.
    You'll get the short date: 03/26/24.
    2. Now in the other direction, from the form to the sheet. Force the correct data type:
    Please Login or Register  to view this content.
    A common mistake is to try to force a format when passing a value, e.g.
    Please Login or Register  to view this content.
    You shouldn't do that, because text is passed to the cell, and there should be a date (the Format function returns text). If you want the cell to display the date in a different format (e.g. "mm/dd/yy") then you should format the cell accordingly, not the passed value! Implicit conversion of text to numbers on the VBA-Excel border is a separate issue. Do not rely on implicit conversion, as this can lead to errors. If you use the code:
    Please Login or Register  to view this content.
    my guess is that you will see 03/26/2024 in the formula bar, but if you want the cell to display a different format, format it accordingly.

    Once you are sure that cell A7 stores the date, your code for exporting to a PDF file will look like this:
    Please Login or Register  to view this content.
    You don't have to add the extension in the file name, because you first force the type of the exported file (xlTypePDF).

    Artik
    Last edited by Artik; 03-27-2024 at 10:14 AM.

  7. #7
    Registered User
    Join Date
    10-10-2003
    Location
    Massachusetts
    MS-Off Ver
    Office 365
    Posts
    88

    Re: Date torture

    Got it.

    TY!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 03-21-2018, 09:25 AM
  2. [SOLVED] Change Date with Date Picker If Date Less Than 7 Days From Another Date
    By Macfool in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-16-2016, 09:10 AM
  3. Week to date, Month to date, Qtr to date and Year to date
    By Neilesh Kumar in forum Excel General
    Replies: 4
    Last Post: 06-10-2016, 08:53 AM
  4. Counting if data falls on date between Start Date, End Date or Possible End Date
    By JessHasQuestions in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 07-26-2014, 08:01 AM
  5. Replies: 1
    Last Post: 10-02-2012, 02:42 PM
  6. Replies: 1
    Last Post: 09-28-2012, 08:52 AM
  7. Replies: 7
    Last Post: 11-16-2008, 05:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1