Results 1 to 5 of 5

Thread: [RESOLVED] Excel VBA - Format date regardless Windows regional settings

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2020
    Posts
    3

    Resolved [RESOLVED] Excel VBA - Format date regardless Windows regional settings

    Hi

    I want to "decode" date properly regardless regional settings.
    I want to concatenate day, month and year and be sure date will be properly calculated.

    I figure it out that way:

    Dim d1 As String
    Dim d2 As String

    d1 = #10/12/2020#
    MsgBox Format(d1, "dd.mm.yyyy")

    d2 = "#" & "10" & "/" & "12" & "/" & "2020" & "#"
    MsgBox Format(d2, "dd.mm.yyyy")

    Why concatenate in d2 is not working (i want to put days, months and years separately into format function)
    d1 is calculated OK, but d2 showing #10/12/2020# and its not a date i can use.

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2020
    Posts
    3

    Re: Excel VBA - Format date regardless Windows regional settings

    OK, i figured it out.
    I used DateSerial function.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Excel VBA - Format date regardless Windows regional settings

    try like
    Code:
    msgbox format(dateserial(2020, 12, 10), "dd.mm.yyyy")  ' 10th december
    i might have your month and day mixed up
    date serial is unambiguous and returns a date value, not a string

    d1 is a string converted from a date, but could be ambiguous
    d2 is a string from a concatenated string, try d1 = "#10/12/2020#" and d1 = "10/12/2020", just to see the results
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2020
    Posts
    3

    Re: [RESOLVED] Excel VBA - Format date regardless Windows regional settings

    Quote Originally Posted by westconn1 View Post
    try like
    Code:
    msgbox format(dateserial(2020, 12, 10), "dd.mm.yyyy")  ' 10th december
    i might have your month and day mixed up
    date serial is unambiguous and returns a date value, not a string

    d1 is a string converted from a date, but could be ambiguous
    d2 is a string from a concatenated string, try d1 = "#10/12/2020#" and d1 = "10/12/2020", just to see the results

    I don't think d1 may be ambiguous.
    d1 always means "12 october", same as dateserial(2020, 12, 10)

    It's easy to get date from format(dataserial(right(A2,4), left(A3,2)...etc.

    But i dont know how to concatenate #10/12/2020#, because d1 works, but d2 which is formally #10/12/2020# too, doesn't work.

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] Excel VBA - Format date regardless Windows regional settings

    you could try
    Code:
    d2 = CDate("10" & "/" & "12" & "/" & "2020")
    but it may be affected by regional settings, dateserial wold be better
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width