Stop changing date value in VBA
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
add a comment |
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
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 domm/dd/yyyydoes the date/month switch like it "should"?
– BruceWayne
Nov 13 '18 at 17:13
add a comment |
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
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
excel vba
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 domm/dd/yyyydoes the date/month switch like it "should"?
– BruceWayne
Nov 13 '18 at 17:13
add a comment |
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 domm/dd/yyyydoes 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
add a comment |
2 Answers
2
active
oldest
votes
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)
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 usecell = CDATE(ctrl.Value)I guess. I also make use of the controlsTagproperty 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
add a comment |
When you're about to write your date to the sheet:
- Verify that it's being stored as a date (or date equivalent) with
isdate() - Assign it via
Range.FormulaLocalinstead ofvalueorvalue2.
Untested, but i.e.
Range("A1").FormulaLocal = #10/31/2018#
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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 usecell = CDATE(ctrl.Value)I guess. I also make use of the controlsTagproperty 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
add a comment |
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)
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 usecell = CDATE(ctrl.Value)I guess. I also make use of the controlsTagproperty 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
add a comment |
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)
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)
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 usecell = CDATE(ctrl.Value)I guess. I also make use of the controlsTagproperty 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
add a comment |
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 usecell = CDATE(ctrl.Value)I guess. I also make use of the controlsTagproperty 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
add a comment |
When you're about to write your date to the sheet:
- Verify that it's being stored as a date (or date equivalent) with
isdate() - Assign it via
Range.FormulaLocalinstead ofvalueorvalue2.
Untested, but i.e.
Range("A1").FormulaLocal = #10/31/2018#
add a comment |
When you're about to write your date to the sheet:
- Verify that it's being stored as a date (or date equivalent) with
isdate() - Assign it via
Range.FormulaLocalinstead ofvalueorvalue2.
Untested, but i.e.
Range("A1").FormulaLocal = #10/31/2018#
add a comment |
When you're about to write your date to the sheet:
- Verify that it's being stored as a date (or date equivalent) with
isdate() - Assign it via
Range.FormulaLocalinstead ofvalueorvalue2.
Untested, but i.e.
Range("A1").FormulaLocal = #10/31/2018#
When you're about to write your date to the sheet:
- Verify that it's being stored as a date (or date equivalent) with
isdate() - Assign it via
Range.FormulaLocalinstead ofvalueorvalue2.
Untested, but i.e.
Range("A1").FormulaLocal = #10/31/2018#
answered Nov 13 '18 at 20:10
chillinchillin
1,459134
1,459134
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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/yyyydoes the date/month switch like it "should"?– BruceWayne
Nov 13 '18 at 17:13