Stop changing date value in VBA










0















Excel keeps changing my date to US format. I have a user form where the user inputs a date. As an example, I enter 01/11/2018 (1st November 2018). I then put this value into a cell however it changes automatically to 11/01/2018 or 11th January 2018.



I've so far tried using the following code to force it to use UK format;



SHT_data_TASKS.Cells(1 + tableRows, 8).NumberFormat = "dd/mm/yyyy;@"
SHT_data_TASKS.Cells(1 + tableRows, 8) = form_addTask_Date.Value


But it does not help. I've also tried using CDate function but still no luck. I've checked my computer regional settings and they are all correct for the UK.



If I right click on the cell where the date is placed and chose format it shows as already being formatted to UK date! I'm not sure what is causing it to change.










share|improve this question

















  • 1





    Is your PC set to the correct locality?

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:09











  • Yes all UK settings in control panel Region and Language

    – ChrisBull
    Nov 13 '18 at 17:11











  • Dumb question - if you do mm/dd/yyyy does the date/month switch like it "should"?

    – BruceWayne
    Nov 13 '18 at 17:13















0















Excel keeps changing my date to US format. I have a user form where the user inputs a date. As an example, I enter 01/11/2018 (1st November 2018). I then put this value into a cell however it changes automatically to 11/01/2018 or 11th January 2018.



I've so far tried using the following code to force it to use UK format;



SHT_data_TASKS.Cells(1 + tableRows, 8).NumberFormat = "dd/mm/yyyy;@"
SHT_data_TASKS.Cells(1 + tableRows, 8) = form_addTask_Date.Value


But it does not help. I've also tried using CDate function but still no luck. I've checked my computer regional settings and they are all correct for the UK.



If I right click on the cell where the date is placed and chose format it shows as already being formatted to UK date! I'm not sure what is causing it to change.










share|improve this question

















  • 1





    Is your PC set to the correct locality?

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:09











  • Yes all UK settings in control panel Region and Language

    – ChrisBull
    Nov 13 '18 at 17:11











  • Dumb question - if you do mm/dd/yyyy does the date/month switch like it "should"?

    – BruceWayne
    Nov 13 '18 at 17:13













0












0








0








Excel keeps changing my date to US format. I have a user form where the user inputs a date. As an example, I enter 01/11/2018 (1st November 2018). I then put this value into a cell however it changes automatically to 11/01/2018 or 11th January 2018.



I've so far tried using the following code to force it to use UK format;



SHT_data_TASKS.Cells(1 + tableRows, 8).NumberFormat = "dd/mm/yyyy;@"
SHT_data_TASKS.Cells(1 + tableRows, 8) = form_addTask_Date.Value


But it does not help. I've also tried using CDate function but still no luck. I've checked my computer regional settings and they are all correct for the UK.



If I right click on the cell where the date is placed and chose format it shows as already being formatted to UK date! I'm not sure what is causing it to change.










share|improve this question














Excel keeps changing my date to US format. I have a user form where the user inputs a date. As an example, I enter 01/11/2018 (1st November 2018). I then put this value into a cell however it changes automatically to 11/01/2018 or 11th January 2018.



I've so far tried using the following code to force it to use UK format;



SHT_data_TASKS.Cells(1 + tableRows, 8).NumberFormat = "dd/mm/yyyy;@"
SHT_data_TASKS.Cells(1 + tableRows, 8) = form_addTask_Date.Value


But it does not help. I've also tried using CDate function but still no luck. I've checked my computer regional settings and they are all correct for the UK.



If I right click on the cell where the date is placed and chose format it shows as already being formatted to UK date! I'm not sure what is causing it to change.







excel vba






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 17:05









ChrisBullChrisBull

189215




189215







  • 1





    Is your PC set to the correct locality?

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:09











  • Yes all UK settings in control panel Region and Language

    – ChrisBull
    Nov 13 '18 at 17:11











  • Dumb question - if you do mm/dd/yyyy does the date/month switch like it "should"?

    – BruceWayne
    Nov 13 '18 at 17:13












  • 1





    Is your PC set to the correct locality?

    – Darren Bartrup-Cook
    Nov 13 '18 at 17:09











  • Yes all UK settings in control panel Region and Language

    – ChrisBull
    Nov 13 '18 at 17:11











  • Dumb question - if you do mm/dd/yyyy does the date/month switch like it "should"?

    – BruceWayne
    Nov 13 '18 at 17:13







1




1





Is your PC set to the correct locality?

– Darren Bartrup-Cook
Nov 13 '18 at 17:09





Is your PC set to the correct locality?

– Darren Bartrup-Cook
Nov 13 '18 at 17:09













Yes all UK settings in control panel Region and Language

– ChrisBull
Nov 13 '18 at 17:11





Yes all UK settings in control panel Region and Language

– ChrisBull
Nov 13 '18 at 17:11













Dumb question - if you do mm/dd/yyyy does the date/month switch like it "should"?

– BruceWayne
Nov 13 '18 at 17:13





Dumb question - if you do mm/dd/yyyy does the date/month switch like it "should"?

– BruceWayne
Nov 13 '18 at 17:13












2 Answers
2






active

oldest

votes


















0














This is the date code I use on control containing UK dates:



Private Sub UserForm_Initialize()
Me.txtDate = Format(Date, "dd-mmm-yyyy")
End Sub


On the AfterUpdate event for the date control:



Private Sub txtDate_AfterUpdate()
With Me
FormatDate .txtDate
End With
End Sub


In a normal module:



Public Sub FormatDate(ctrl As Control)

Dim dDate As Date
Dim IsDate As Boolean

If Replace(ctrl.Value, " ", "") <> "" Then
On Error Resume Next
dDate = CDate(ctrl.Value)
IsDate = (Err.Number = 0)
Err.Clear
On Error GoTo 0

If IsDate Then
ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
ctrl.BackColor = RGB(255, 255, 255)
Else
ctrl.BackColor = RGB(255, 0, 0)
End If
End If

End Sub


Then I transfer it to the worksheet (on a Save button) using the below code:



shtRawData.Cells(rLastCell.Row, CLng(lCol)) = CDate(ctrl.Value)





share|improve this answer























  • This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

    – ChrisBull
    Nov 14 '18 at 8:58











  • It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

    – Darren Bartrup-Cook
    Nov 14 '18 at 9:10











  • The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

    – Darren Bartrup-Cook
    Nov 14 '18 at 9:15


















0














When you're about to write your date to the sheet:



  1. Verify that it's being stored as a date (or date equivalent) with isdate()

  2. Assign it via Range.FormulaLocal instead of value or value2.

Untested, but i.e.



Range("A1").FormulaLocal = #10/31/2018#





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286167%2fstop-changing-date-value-in-vba%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is the date code I use on control containing UK dates:



    Private Sub UserForm_Initialize()
    Me.txtDate = Format(Date, "dd-mmm-yyyy")
    End Sub


    On the AfterUpdate event for the date control:



    Private Sub txtDate_AfterUpdate()
    With Me
    FormatDate .txtDate
    End With
    End Sub


    In a normal module:



    Public Sub FormatDate(ctrl As Control)

    Dim dDate As Date
    Dim IsDate As Boolean

    If Replace(ctrl.Value, " ", "") <> "" Then
    On Error Resume Next
    dDate = CDate(ctrl.Value)
    IsDate = (Err.Number = 0)
    Err.Clear
    On Error GoTo 0

    If IsDate Then
    ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
    ctrl.BackColor = RGB(255, 255, 255)
    Else
    ctrl.BackColor = RGB(255, 0, 0)
    End If
    End If

    End Sub


    Then I transfer it to the worksheet (on a Save button) using the below code:



    shtRawData.Cells(rLastCell.Row, CLng(lCol)) = CDate(ctrl.Value)





    share|improve this answer























    • This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

      – ChrisBull
      Nov 14 '18 at 8:58











    • It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:10











    • The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:15















    0














    This is the date code I use on control containing UK dates:



    Private Sub UserForm_Initialize()
    Me.txtDate = Format(Date, "dd-mmm-yyyy")
    End Sub


    On the AfterUpdate event for the date control:



    Private Sub txtDate_AfterUpdate()
    With Me
    FormatDate .txtDate
    End With
    End Sub


    In a normal module:



    Public Sub FormatDate(ctrl As Control)

    Dim dDate As Date
    Dim IsDate As Boolean

    If Replace(ctrl.Value, " ", "") <> "" Then
    On Error Resume Next
    dDate = CDate(ctrl.Value)
    IsDate = (Err.Number = 0)
    Err.Clear
    On Error GoTo 0

    If IsDate Then
    ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
    ctrl.BackColor = RGB(255, 255, 255)
    Else
    ctrl.BackColor = RGB(255, 0, 0)
    End If
    End If

    End Sub


    Then I transfer it to the worksheet (on a Save button) using the below code:



    shtRawData.Cells(rLastCell.Row, CLng(lCol)) = CDate(ctrl.Value)





    share|improve this answer























    • This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

      – ChrisBull
      Nov 14 '18 at 8:58











    • It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:10











    • The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:15













    0












    0








    0







    This is the date code I use on control containing UK dates:



    Private Sub UserForm_Initialize()
    Me.txtDate = Format(Date, "dd-mmm-yyyy")
    End Sub


    On the AfterUpdate event for the date control:



    Private Sub txtDate_AfterUpdate()
    With Me
    FormatDate .txtDate
    End With
    End Sub


    In a normal module:



    Public Sub FormatDate(ctrl As Control)

    Dim dDate As Date
    Dim IsDate As Boolean

    If Replace(ctrl.Value, " ", "") <> "" Then
    On Error Resume Next
    dDate = CDate(ctrl.Value)
    IsDate = (Err.Number = 0)
    Err.Clear
    On Error GoTo 0

    If IsDate Then
    ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
    ctrl.BackColor = RGB(255, 255, 255)
    Else
    ctrl.BackColor = RGB(255, 0, 0)
    End If
    End If

    End Sub


    Then I transfer it to the worksheet (on a Save button) using the below code:



    shtRawData.Cells(rLastCell.Row, CLng(lCol)) = CDate(ctrl.Value)





    share|improve this answer













    This is the date code I use on control containing UK dates:



    Private Sub UserForm_Initialize()
    Me.txtDate = Format(Date, "dd-mmm-yyyy")
    End Sub


    On the AfterUpdate event for the date control:



    Private Sub txtDate_AfterUpdate()
    With Me
    FormatDate .txtDate
    End With
    End Sub


    In a normal module:



    Public Sub FormatDate(ctrl As Control)

    Dim dDate As Date
    Dim IsDate As Boolean

    If Replace(ctrl.Value, " ", "") <> "" Then
    On Error Resume Next
    dDate = CDate(ctrl.Value)
    IsDate = (Err.Number = 0)
    Err.Clear
    On Error GoTo 0

    If IsDate Then
    ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
    ctrl.BackColor = RGB(255, 255, 255)
    Else
    ctrl.BackColor = RGB(255, 0, 0)
    End If
    End If

    End Sub


    Then I transfer it to the worksheet (on a Save button) using the below code:



    shtRawData.Cells(rLastCell.Row, CLng(lCol)) = CDate(ctrl.Value)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 13 '18 at 17:21









    Darren Bartrup-CookDarren Bartrup-Cook

    13.9k11432




    13.9k11432












    • This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

      – ChrisBull
      Nov 14 '18 at 8:58











    • It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:10











    • The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:15

















    • This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

      – ChrisBull
      Nov 14 '18 at 8:58











    • It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:10











    • The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

      – Darren Bartrup-Cook
      Nov 14 '18 at 9:15
















    This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

    – ChrisBull
    Nov 14 '18 at 8:58





    This seems to have solved it - seems unnecessarily long winded just to get a date in the format it is typed but it is at least working - thanks!

    – ChrisBull
    Nov 14 '18 at 8:58













    It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

    – Darren Bartrup-Cook
    Nov 14 '18 at 9:10





    It is a bit, but it tests that an actual date is entered to the control and changes the colour if it isn't. Without the tests you could just use cell = CDATE(ctrl.Value) I guess. I also make use of the controls Tag property to hold the column number to save to & dictate what kind of data can be entered to the control.

    – Darren Bartrup-Cook
    Nov 14 '18 at 9:10













    The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

    – Darren Bartrup-Cook
    Nov 14 '18 at 9:15





    The answer given by @chilliin would be worth a look. Haven't tried it on a form, but it should work.

    – Darren Bartrup-Cook
    Nov 14 '18 at 9:15













    0














    When you're about to write your date to the sheet:



    1. Verify that it's being stored as a date (or date equivalent) with isdate()

    2. Assign it via Range.FormulaLocal instead of value or value2.

    Untested, but i.e.



    Range("A1").FormulaLocal = #10/31/2018#





    share|improve this answer



























      0














      When you're about to write your date to the sheet:



      1. Verify that it's being stored as a date (or date equivalent) with isdate()

      2. Assign it via Range.FormulaLocal instead of value or value2.

      Untested, but i.e.



      Range("A1").FormulaLocal = #10/31/2018#





      share|improve this answer

























        0












        0








        0







        When you're about to write your date to the sheet:



        1. Verify that it's being stored as a date (or date equivalent) with isdate()

        2. Assign it via Range.FormulaLocal instead of value or value2.

        Untested, but i.e.



        Range("A1").FormulaLocal = #10/31/2018#





        share|improve this answer













        When you're about to write your date to the sheet:



        1. Verify that it's being stored as a date (or date equivalent) with isdate()

        2. Assign it via Range.FormulaLocal instead of value or value2.

        Untested, but i.e.



        Range("A1").FormulaLocal = #10/31/2018#






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 20:10









        chillinchillin

        1,459134




        1,459134



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286167%2fstop-changing-date-value-in-vba%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands