Consulting

Results 1 to 12 of 12

Thread: How Do I Change The Date Format in VBA Code

  1. #1

    How Do I Change The Date Format in VBA Code

    Hi All, I have some Code which works apart from when I want to name a file with the date (IE Advice Note - 20/04/2018.pdf) it keeps crashing. i know the reason why because your not allowed to use "/" in the name of a file but how do you change it in the coding to show like the following Advice Note_20-04-2018.pdf
    I have put my code below for your viewing so you can see how i've created the code.

    Thanks in Advance

    Sub PrintAdviceNote()
    
    ProjectRootFolder = Sheets("RootFolder").Range("B5").Value
    JobNumber = Sheets("Summary").Range("CJobNumber").Value
    CSiteName = Sheets("Summary").Range("CSiteName").Value
    CCompanyName = Sheets("Summary").Range("CCompanyName").Value
    AdviceNote = Sheets("AdviceNote").Range("DelNote").Value
    
    
        
        
        Sheets("AdviceNote").Select
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        
        With Sheets("AdviceNote")
            .Visible = True
            .ExportAsFixedFormat Type:=xlTypePDF, _
                                 FileName:=ProjectRootFolder & Application.PathSeparator & _
                                           CCompanyName & Application.PathSeparator & _
                                           JobNumber & " " & CSiteName & Application.PathSeparator & _
                                           AdviceNote & "_" & Date & ".pdf", _
                                           Quality:=xlQualityStandard, _
                                           IncludeDocProperties:=True, _
                                           IgnorePrintAreas:=False, _
                                           OpenAfterPublish:=True
                                            .Visible = True
        End With
        Sheets("Summary").Select
        ActiveWorkbook.Save
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This is function from my utility belt that 'cleans' file and folder names by replacing illegal characters with an underscore

    There are some other characters besides \ that are not allowed, so I thought I'd make it as robust as possible

    I also decided that I wanted spaces replaced with underscores also, but that's changeable

    It doesn't run very often so performance isn't an issue so I tried to make it as straight forward as possible

    Function fileCleanName(s As String, Optional ThisIsAFolder As Boolean = False) As String
        Dim s1 As String, s2 As String
        Dim i As Long
        
        
        s1 = vbNullString
        s2 = Trim(s)
        
        If Not ThisIsAFolder Then
            For i = 1 To Len(s2)
                Select Case Mid(s2, i, 1)
                    Case Chr(0) To Chr(32), "<", ">", ":", """", "/", "\", "|", "?", "*"
                        s1 = s1 & "_"
                    Case Else
                        s1 = s1 & Mid(s2, i, 1)
                End Select
            Next i
        
        'paths can have / and \
        Else
            For i = 1 To Len(s2)
                Select Case Mid(s2, i, 1)
                    Case Chr(0) To Chr(32), "<", ">", ":", """",  "|", "?", "*"
                        s1 = s1 & "_"
                    Case Else
                        s1 = s1 & Mid(s2, i, 1)
                End Select
            Next i
        End If
        
        Do While InStr(s1, "__") > 0
            s1 = Replace(s1, "__", "_")
        Loop
        
        fileCleanName = s1
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    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

  3. #3
    Thanks Paul,

    When would i call this function in to action??

    Nathan

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by nathandavies View Post
    Thanks Paul,

    When would i call this function in to action??

    Nathan

    Just like any other function

    Sub PrintAdviceNote()
    ProjectRootFolder = Sheets("RootFolder").Range("B5").Value
    JobNumber = Sheets("Summary").Range("CJobNumber").Value
    CSiteName = Sheets("Summary").Range("CSiteName").Value
    CCompanyName = Sheets("Summary").Range("CCompanyName").Value
    AdviceNote = Sheets("AdviceNote").Range("DelNote").Value
    
    filenametosave = ProjectRootFolder & Application.PathSeparator
    filenametosave = filenametosave & CCompanyName & Application.PathSeparator
    filenametosave = filenametosave & JobNumber & " " & CSiteName & Application.PathSeparator
    filenametosave = filenametosave & AdviceNote & "_" & Format(Date, "dd-mm-yyyy") & ".pdf"
        
    MsgBox filenametosave
    
    
    filenametosave = cleanfilename(filenametosave)
    MsgBox filenametosave
        
        
        Sheets("AdviceNote").Select
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        
        With Sheets("AdviceNote")
            .Visible = True
            .ExportAsFixedFormat Type:=xlTypePDF, _
                                 Filename:=filenametosave, _
                                           Quality:=xlQualityStandard, _
                                           IncludeDocProperties:=True, _
                                           IgnorePrintAreas:=False, _
                                           OpenAfterPublish:=True
                                            .Visible = True
        End With
        Sheets("Summary").Select
        ActiveWorkbook.Save
    End Sub
     
    Function fileCleanName(s As String, Optional ThisIsAFolder As Boolean = False) As String
        Dim s1 As String, s2 As String
        Dim i As Long
        
        
        s1 = vbNullString
        s2 = Trim(s)
        
        If Not ThisIsAFolder Then
            For i = 1 To Len(s2)
                Select Case Mid(s2, i, 1)
                    Case Chr(0) To Chr(32), "<", ">", ":", """", "/", "\", "|", "?", "*"
                        s1 = s1 & "_"
                    Case Else
                        s1 = s1 & Mid(s2, i, 1)
                End Select
            Next i
        
        'paths can have / and \
        Else
            For i = 1 To Len(s2)
                Select Case Mid(s2, i, 1)
                    Case Chr(0) To Chr(32), "<", ">", ":", """", "|", "?", "*"
                        s1 = s1 & "_"
                    Case Else
                        s1 = s1 & Mid(s2, i, 1)
                End Select
            Next i
        End If
        
        Do While InStr(s1, "__") > 0
            s1 = Replace(s1, "__", "_")
        Loop
        
        fileCleanName = s1
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    Hi Paul,
    i have tried the code and i keep getting an compile error. "Sub or Fuction not Defined"

    filenametosave = cleanfilename(filenametosave)

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Should it be

    filenametosave = fileCleanName(filenametosave)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Thanks xld, I didn’t notice that, I now have the following error "ByRef argument type mismatch"

    (filenametosave)

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You need to declare filenametosave as a string variable, as it is undeclared it is being treated as a variant.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Thanks for your help, this has worked but is now not saving the PDF in the correct location, i think its because its removing all the "" & "/" from the patch and not the file name only.

  10. #10
    Ive managed to fix the problem.

    Thank you for your help!

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by nathandavies View Post
    Hi Paul,
    i have tried the code and i keep getting an compile error. "Sub or Fuction not Defined"

    filenametosave = cleanfilename(filenametosave)

    My fault -- Careless mistake

    I always use Option Explicit for my stuff, but followed your lead and didn't use it since no variables were Dim-ed

    Since I couldn't test it, the macro never got a chance to fail
    ---------------------------------------------------------------------------------------------------------------------

    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

  12. #12
    Paul, not a problem thank you for you help on this!

Posting Permissions

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