Consulting

Results 1 to 6 of 6

Thread: Working with Dates in VBA

  1. #1
    VBAX Regular
    Joined
    Jul 2019
    Posts
    51
    Location

    Working with Dates in VBA

    I have a set of files that follow the following naming scheme:SOP_Audit-JV-006-02182019.docx

    The last part of the file name is a date that I want to pull into VBA, which I have working fine using Split(). Is there something similar to this: https://www.php.net/manual/en/function.date.php in VBA? Something that would allow me to convert that string to a date and then be able to format it differently, or compare it with another date, break it down into just the month or year, etc.

    I was reading about the CDate function and trying to use that but am having some issues with it.

    Thank you!

  2. #2
    Maybe
    Sub mongoose()
       Dim St As String
       Dim Dt As Date
       St = Mid(ActiveCell, InStrRev(ActiveCell, "-") + 1, 8)
       
       Dt = DateSerial(Right(St, 4), Left(St, 2), Mid(St, 3, 2))
    End Sub

  3. #3
    VBAX Regular
    Joined
    Jul 2019
    Posts
    51
    Location
    Ya I just did it like so:

    Dim auditDate: auditDate = CDate(DateSerial(Right(Left(Split(oFile.Name, "-")(3), 8), 4), _                                                        Left(Left(Split(oFile.Name, "-")(3), 8), 2), _
                                                            Mid(Left(Split(oFile.Name, "-")(3), 8), 3, 2)))
    What do you think?

  4. #4
    I'd assign the date string to a variable
    Dim sp As Variant
    Dim auditDate As Date
    
    sp = Left(Split(oFile.Name, "-")(3), 8)
    auditDate = CDate(DateSerial(Right(sp, 4), Left(sp, 2), Mid(sp, 3, 2)))

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    fName = "SOP_Audit-JV-006-02182019.docx"
    x = Left(fName, InStrRev(fName, ".") - 1) 'x contains everything before the last dot.
    strDate = Mid(x, InStrRev(x, "-") + 1) 'strDate contains everything after the last - in x.
    dd = DateSerial(Mid(strDate, 5), Left(strDate, 2), Mid(strDate, 3, 2)) 'dd contains the date.
    edit postposting: Shucks, beaten to it.
    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.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quote Originally Posted by mongoose View Post
    Ya I just did it like so:

    Dim auditDate: auditDate = CDate(DateSerial(Right(Left(Split(oFile.Name, "-")(3), 8), 4), _                                                        Left(Left(Split(oFile.Name, "-")(3), 8), 2), _
                                                            Mid(Left(Split(oFile.Name, "-")(3), 8), 3, 2)))
    What do you think?

    I'd Dim auditDate As Date
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

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
  •