+ Reply to Thread
Results 1 to 4 of 4

DateValue function presenting unexpected results in VBA

  1. #1
    Registered User
    Join Date
    05-06-2019
    Location
    Dartford, England
    MS-Off Ver
    Office 365
    Posts
    2

    DateValue function presenting unexpected results in VBA

    Hi folks,

    I'd appreciate some help here. I've spent a lot of time reading up and understanding the challenges around dates in Excel. So, when I came across this challenge, I thought it would be an easy fix. A bunch of dates in a spreadsheet have become messed up because during a migration one or other of the systems had the wrong locale set causing dates in a UK format to be incorrectly recognized as US Dates. So now there’s a bunch of dates in the sheet, some correct because the high 'day' value seen as an invalid 'month' in US format made the date go in as text (Ironically these dates are correct as they look), but the low day values are recognized as months and form valid dates. For example, the date "7th of Jan" (7/1/2019) is in the sheet as July 1st, and stored as a proper date value i.e. 43647.

    Fortunately, in the sheet there was a column whereby the correct month number was entered on its own so I figured I could do a 'DateFixer' macro that simply converts the dates stored as text into date values with a simple datevalue() function and for the cells stored as a date values, convert them to text, check against the column with the month number and swap the Month and Day in the dates that need it then resolve them back as dates values. Sounds straight, forward right?!

    Well no. Cell A1 holds a date stored as a string (left aligned). I run the line of code:


    Please Login or Register  to view this content.

    Result, 43516, which when formatted as a date shows as 20/02/2019 (right aligned)

    But, I put the same steps into a variable and...


    Please Login or Register  to view this content.

    changes nothing! Can anyone explain why? And indeed how do I use variable to get the results achieved with the literal reference to Range("A1")?
    Last edited by Coldry; 05-07-2019 at 12:05 PM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: DateValue function presenting unexpected results in VBA

    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 [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    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.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    05-06-2019
    Location
    Dartford, England
    MS-Off Ver
    Office 365
    Posts
    2

    Re: DateValue function presenting unexpected results in VBA

    Thank you alansidman, please accept my apologies, I didn't properly read the rules I'm sure it tells me somewhere in there to do that.

    And I think I figured out what was going on. It looks like when I set the objCell to be equal to Range("A1") objCell becomes a model of the Excel Cell with all its properties. The Watch window just happens to be showing the 'Value' Property. So when I was setting "objCell = DateValue(objCell)" I was simply blatting the object and making it a number with no reference to the Excel Cell anymore!

    The literal one worked because you're not allowed to blat the built-in range object so the 'Set' command was forced to put its result into the Value property of the object. Well, that's what I think was going on because the following code appeared to work as I expected.

    Please Login or Register  to view this content.
    Although there was still an anomaly I didn't understand. If I tried to set an object variable to be equal to 'Selection' and then used the variable in the For Each command, that wouldn't work either. So please if anyone understands why that might be the case, please enlighten us all

    Also, I noticed after posting this, there was a separate VBA channel if it's possible to move this thread please tell me how. :-)

  4. #4
    Forum Expert
    Join Date
    10-15-2018
    Location
    MA, USA
    MS-Off Ver
    2010, 2019
    Posts
    1,616

    Re: DateValue function presenting unexpected results in VBA

    As the moderation request has been addressed i think I'm OK to respond.


    If I tried to set an object variable to be equal to 'Selection' and then used the variable in the For Each command, that wouldn't work
    What do you mean by "wouldn't work". An error? An unexpected result?

    I tried the following which worked correctly for me.

    Please Login or Register  to view this content.
    If the above code addresses your remaining issue then simply mark the thread as solved. If you still have a question then yes, this thread should probably be moved to the VBA section. You can't do that yourself. Click on "Report post", make your request and a moderator or admin will do it for you.

+ 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. Creating string with for loop and if function - unexpected results
    By eg0e in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2015, 10:15 AM
  2. Use COUNTIFS function on results of DATEVALUE function?
    By Si902 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-16-2015, 08:13 PM
  3. LINEST function unexpected results
    By guffaw320 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-05-2013, 02:46 PM
  4. Offset function with unexpected results
    By Coaster in forum Excel General
    Replies: 10
    Last Post: 07-01-2010, 04:09 AM
  5. INT function returns unexpected results
    By 6StringJazzer in forum Excel General
    Replies: 9
    Last Post: 03-24-2010, 08:24 PM
  6. Comparing two or workbooks and presenting results
    By craighaylett in forum Excel General
    Replies: 5
    Last Post: 11-19-2009, 11:12 AM
  7. The ispmt function is providing unexpected results
    By Louis Zaffino in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-02-2006, 11:10 PM

Tags for this Thread

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