+ Reply to Thread
Results 1 to 18 of 18

Convert date string to serial number

  1. #1
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Convert date string to serial number

    I've been banging on this for a month. I had it working on a template using dates from December and then we updated the dates for January and it's not working. Here's where I'm having trouble:
    Please Login or Register  to view this content.
    Because I was having trouble finding the phrase 12/17/2013 (which was actually a formula and even specifying Lookin:=xlValues didn't work), I have hidden rows that display the dates as their date serials. This works great for other parts of the macro that need to find the dates. However, at those points the dates are being grabbed from a listbox which seems to be giving it to the variable holder as the serial number. This bit of code above is happening before the listbox is populated, so the dates are strings. How do I get the macro to convert those strings to the serials?

    I found two different articles that said I should be able to use DATEVALUE but so far that doesn't seem to be working, either in the macro or in the worksheet as a formula. It seems to just return whatever it gets: 12/17/2013 in, "12/17/2013" out.
    TechOnTheNet article
    office.microsoft.com article

    TIA,
    ama

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    Try,

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    That gives me run-time error 13. A1 is the whole phrase "12/17/2013 to 12/24/2013" (or whatever date range was chosen when the file was pulled). So I moved it down and tried:
    Please Login or Register  to view this content.
    Still got type mismatch error.

    Edit: Which just occured to me is probably because of the "/" making Excel see text rather than a date?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    "12/17/2013 to 12/24/2013" can not be convert in to serial number.

  5. #5
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    Yep. Which is why my code, if you read it all, actually breaks out the dates.
    This is the start date, 12/17/2013:
    Please Login or Register  to view this content.
    This is the end date, 12/24/2013:
    Please Login or Register  to view this content.
    BUT those aren't even used in my search string. What's used is the textbox entry, DtSt. The textbox value comes from a date picker userform. The date picker puts 12/17/2013 into the textbox, which is being passed to the variable as a text string, from what I can see.

    Perhaps I should have dropped the xl.. variable stuff. I was in a hurry and just copied the whole section. My apologies.

    Just for the record, I did go to the DtSt line and try:
    Please Login or Register  to view this content.
    and still got type mismatch.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    I only two functions which can convert a string in to numbers.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    I will try that in the morning. I'm lowly hourly and have to clock out . Thanks so much.

  8. #8
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    Good morning. I tried:
    Please Login or Register  to view this content.
    and changed the search line to:
    Please Login or Register  to view this content.
    and I'm getting error code 91. It's searching for 12/17/2013. When I hover over ControlPanel.tbxStartDte, I see the value as:
    Please Login or Register  to view this content.
    When I hover over DtSt, I see it's value as:
    Please Login or Register  to view this content.
    So CDate is working on the string and converting it.

    But what I really need is how to get 12/17/2013 converted to its serial: 41625. All the dates displayed in the workbook are actually formulas and for whatever reason, the search always fails unless I'm searching for the serial.

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    Try to remove value and see what says

    DtSt = CLng(ControlPanel.tbxStartDte)

  10. #10
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    I did. That's what's in my reply above. I even tried with value and still what it's giving me is 12/17/2013 without quotation marks.

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    is DtSt a variable? If so how is it defined?
    Am I right that when you search, the code returns nothing as the data to be searched are formatted as dates while the Dtst is a string, so there is a mismatch?

  12. #12
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    It is and declared as date, and no the error that's coming back is 91 - the variable or with block is not set. I've stepped through hovering over every variable and they're all set.

  13. #13
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Convert date string to serial number

    Are dates in the worksheet the result of formulas or literals?
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  14. #14
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    Okay!

    This line may be the culprit

    Cells.Find(what:=DtSt).Activate

    Dim rng as range
    You need to set rng as an object. Like
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    Izandol - The worksheet displays a month of dates and all except the first date of the month are formulas: Date 1: 12/1/2013, Date 2: =previous cell + 1, Date 3: = previous cell + 1... and so on. Just FYI, if you're going to suggest changing LookIn to xlValues, I have and it still doesn't find the date.

    AB33 - I'm not sure why setting the search to a range variable would matter. Could you explain please? I'm confused because, like I said before, I've used the same line in other parts of the macro successfully. In the meantime, I'll give it a shot.

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    It is the error message you are getting. I also wanted to test which line of the code errors.
    Does the code errors on that line? The code should not error if the find function syntax is correct. Yes, it will not return a value if the find does not find match because there is a mismatch between dates and string.

  17. #17
    Registered User
    Join Date
    06-27-2012
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Convert date string to serial number

    So basically I've been using Find incorrectly *facepalm*. That was the line it was erroring on and with your changes it's working now :D. It finds the date and doesn't find it when it shouldn't. Thank you! Seriously :D.

  18. #18
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Convert date string to serial number

    amalfaro,
    With the wonderful world of hindsight, it would have been easy to spot the error if we had looked at the entire code.
    Yes, you should use set with find method.
    You are welcome!

+ 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. how to convert date serial number to regular date?
    By union in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-21-2013, 10:15 PM
  2. Convert date and time in serial number format to text format
    By nda13112 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-11-2013, 02:45 PM
  3. Converting a date serial number back to a date within a string
    By bharris77 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-22-2012, 01:15 PM
  4. Convert Serial Number to Date WITH TEXT
    By HP RodNuclear in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-20-2010, 12:39 PM
  5. How do I convert Date serial number to date
    By rdunne in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-12-2005, 11:06 AM

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