Consulting

Results 1 to 4 of 4

Thread: Solved: Creating and naming a file

  1. #1
    VBAX Regular
    Joined
    Jun 2005
    Posts
    95
    Location

    Solved: Creating and naming a file

    Hello again,

    I am trying to figure out if there is a way use VBA code to create a new file folder and name it as the last year (if the current year is 2008, I want to name the file folder "2007"). Is this possible?

  2. #2
    Here s a hint

    [VBA]Sub MakeFolder()

    Dim sThisYear As String

    sThisYear = CStr(DatePart("yyyy", Now))

    'Make sure that the root exists
    MkDir ("D:\temp\" & sThisYear)



    End Sub

    [/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]
    Sub MakeFolder()
    Const mpFolder As String = "C:\test\saved files\year"
    Dim mpFolders
    Dim i As Long
    Dim mpCreateFolder As String

    mpFolders = Split(mpFolder, Application.PathSeparator)
    mpCreateFolder = mpFolders(LBound(mpFolders))
    On Error Resume Next
    For i = LBound(mpFolders) + 1 To UBound(mpFolders)
    mpCreateFolder = mpCreateFolder & Application.PathSeparator & mpFolders(i)
    MkDir mpCreateFolder
    Next i
    On Error GoTo 0
    MkDir mpCreateFolder & Application.PathSeparator & Format(Date, "yyyy")

    End Sub
    [/vba]
    ____________________________________________
    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

  4. #4
    VBAX Regular
    Joined
    Jun 2005
    Posts
    95
    Location
    You guys are toooooo awesome!!! This worked perfectly! I honestly did not know if it could be done, but thought there had to be a way. Thank you so much for your help! In your code I just added a - 1 to make it the previous year. This is for a checking account program that I designed and this will save me an enormous amount of time when I close out the year. Thank you!

    Sub MakeFolder()
    Const mpFolder As String = "C:\test\saved files\year"
    Dim mpFolders
    Dim i As Long
    Dim mpCreateFolder As String

    mpFolders = Split(mpFolder, Application.PathSeparator)
    mpCreateFolder = mpFolders(LBound(mpFolders))
    On Error Resume Next
    For i = LBound(mpFolders) + 1 To UBound(mpFolders)
    mpCreateFolder = mpCreateFolder & Application.PathSeparator & mpFolders(i)
    MkDir mpCreateFolder
    Next i
    On Error Goto 0
    MkDir mpCreateFolder & Application.PathSeparator & Format(Date, "yyyy") - 1

    End Sub

Posting Permissions

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