+ Reply to Thread
Results 1 to 15 of 15

Excel Macro Not Converting and Formatting Date Properly - Help Please?

  1. #1
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Hi all,

    New here and to Excel macros. I inherited this one and I can't seem to figure out what should be a simple problem. The macro essentially is taking a CSV file and walking through it line by line to create PDF, Excel, and TXT versions of the CSV data.

    The PDF and Excel versions look great. The code looks hacky but it works.

    Here's the input line:

    G1234, , 53.00,50.00,58.00,56.00,56.00,55.00, AA,10012018

    I'm trying to re-output this as a similar CSV but with a properly formatted date for the last item on the line.

    What's output unfortunately is:

    ****G1234**53.00*50.00*58.00*56.00*56.00*55.00*43374.00

    What I WANT output is (note the final item, the date):

    ****G1234**53.00*50.00*58.00*56.00*56.00*55.00*10/01/2018

    Here's the code snip for this part of the macro. Please let me know if you want to see more of the macro than this.
    Please Login or Register  to view this content.


    Help?
    Last edited by AliGW; 04-08-2020 at 12:22 PM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Use DateSerial to create your Date before writing to textfile.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Change this...
    Trim(Left(Cells(J, 13).Text, 2)) & Trim(Mid(Cells(J, 13).Text, 4, 2)) & Trim(Right(Cells(J, 13).Text, 4))

    To this...
    Format(Cells(J, 13).Text, "00/00/0000"
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Really kind of everyone to respond, thanks.

    I think the second solution looks easier to implement since I don't know anything about excel macros or DateSerial. Unfortunately I inherited this code with no Excel macro training.

    Will report back some results.
    Last edited by bdpna; 09-16-2018 at 02:29 PM.

  5. #5
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    I think we're getting so close here but there has to be some funky reason this is converting wrong.

    The date string is the CSV of "1012018" seems to convert to "00043101" or "00/04/3101" using your Format command. The date string is the CSV of "10012018" converts to "00/04/3191". What the heck is going on here? Some sort of ASCII mess?

    So,

    Format(Cells(J, 13).Text, "00/00/0000") of 1012018 = 00/04/3101
    Format(Cells(J, 13).Text, "00/00/0000") of 10012018 = 00/04/3191

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    DateSerial(year, month, day)

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Thank you so much, this is the closest it's ever been! It's killing me because I just don't see how it could be getting the date so wrong. It's definitely a different conversion depending on the date, but I'm wondering if the fact that sometimes the date is 7 characters and sometimes 8 characters matters?

    Here's where we are after that code change. I've never seen it close even in the year format so I'm pleased at that but still baffled at how it's making this leap.

    1012018 = 7/1/2004
    10012018 = 9/12/2007

    Anyone have any other ideas? Really appreciate everyone who took a minute to help, you are nice people.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    If I put this 10012018 in Range A1 and run
    Please Login or Register  to view this content.
    this is what I get.
    10/01/2018

  9. #9
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    I just found an earlier statement in the macro that may answer this issue:

    Range("E2:L" & FinalRow).NumberFormat = "0.00"

    There were a couple columns removed from the input file and this should end at I now, not J. I assume that NumberFormat command may be what is mucking with our dates!

  10. #10
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    That was the problem. I totally missed it when I posted it in the code snippet at the start of the thread. That very first line was causing the date format attempts to be overridden, it tried to format it as currency and it went nutty.

    Thank you, marking solved!

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Glad it sorted out.

  12. #12
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Hey all, resurrecting this old thread as this macro is acting up again. I'll start a new thread if that makes more sense but if anyone has any ideas, I could sure use some help. Between this and quarantine, crazy!

    So this macro is parsing though a CSV file that was built previously, parsing it out into an asterisk-delimited text file.

    One of the fields in the CSV is a date field, but not in the proper format, so I use this command to right it:

    Please Login or Register  to view this content.
    On the mydate command, as it makes its way down the file (it gets around 277 lines in, every time) it hits a 13 mismatch error.

    Can anyone clue me in on what's in my data that would cause this to happen?

    Really appreciate it. At my wit's end!!! Or is there a way to set something to show me all the values here to determine the data error causing crash?
    Last edited by bdpna; 04-08-2020 at 12:26 PM.

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    81,238

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  14. #14
    Registered User
    Join Date
    09-14-2018
    Location
    Eastern US
    MS-Off Ver
    2010
    Posts
    15

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    My bad and thanks! Done! Can the title flag for solved be removed?
    Last edited by bdpna; 04-08-2020 at 12:38 PM.

  15. #15
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Excel Macro Not Converting and Formatting Date Properly - Help Please?

    Did you type in your code in post #12? It has several syntax errors. It wouldn't compile.

    The Debug.Print line will list each row number and cell value in the Immediate window (Ctrl+G). If it errors on one row, it will be the last one listed in the Immediate window. Report back what is in that cell.

    Please Login or Register  to view this content.

+ 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: 1
    Last Post: 10-18-2017, 03:24 PM
  2. Need help in formatting excel data properly
    By rao1995 in forum Excel General
    Replies: 2
    Last Post: 10-05-2016, 01:27 AM
  3. Replies: 0
    Last Post: 12-04-2014, 06:23 PM
  4. [SOLVED] Macro not working properly for conversion of date format
    By abraham30 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-14-2014, 10:46 AM
  5. Converting date column to text formatting
    By mjwillyone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-09-2009, 12:20 PM
  6. Date not formatting properly
    By David Sisson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-19-2006, 06:45 PM
  7. [SOLVED] Date Formatting Converting to Decimal
    By Bob Fasick in forum Excel General
    Replies: 3
    Last Post: 04-15-2006, 02:50 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