Consulting

Results 1 to 7 of 7

Thread: Convert DD. December yyyy string into date?

  1. #1

    Convert DD. December yyyy string into date?

    Hi everybody!

    I have a VBA problem, i would lik to convert the following string into the short date format: "1. december 2015"

    I have tried the obvious solution, which would be to use the CDATE function, but it gives me a mismatch back. Is it possible that a smart way of doing this exists? I would prefer to avoid a loop

    Cheers!

    JKB

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Text-To-Columns:
    Step 1: Delimited
    Step 2: no boxes ticked
    Step 3: Date, DMY
    click Finish.
    Now it's a proper Excel date you can format it as you want.
    Worked here.

    ps. you never did provide a file here: http://www.vbaexpress.com/forum/show...ed!&highlight=
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Okay that works, but it's not very pretty in a code!
    If i were to use text-to-columns, it would be better to use on a string, such that when i insert the value in the sheet, it is already in the date format?

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    What is it you're doing? - a bit more info; where's the string coming from?
    ps. the code's as simple as:
    Selection.TextToColumns DataType:=xlDelimited, FieldInfo:=Array(1, 4)
    Last edited by p45cal; 12-16-2015 at 06:51 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Okay i see that i used a lot of extra arguments! I used this.... (Works but ugly)

    range("A1")TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 4), TrailingMinusNumbers:=True

    I will use yours instead!

    The reason why i would like to do it before pasting, is that my date is in an if statement, and only if my condition holds (which it rarely does), will i have to convert the date using fx text to columns.

    But the part where im pasting the date into my sheet is not part of this loop, which defines the date

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
        MsgBox FormatDateTime(Replace("1. december 2015", ".", ""), 2)
    End Sub

  7. #7
    Quote Originally Posted by snb View Post
    Sub M_snb()
        MsgBox FormatDateTime(Replace("1. december 2015", ".", ""), 2)
    End Sub
    Exactly what i was looking for!!! Thx And sorry about the cursing.

Tags for this Thread

Posting Permissions

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