PDA

View Full Version : Solved: Creating and naming a file



infinity
08-11-2007, 08:42 PM
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?

shasur
08-11-2007, 09:00 PM
Here s a hint

Sub MakeFolder()

Dim sThisYear As String

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

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



End Sub

Bob Phillips
08-12-2007, 01:42 AM
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

infinity
08-12-2007, 07:45 PM
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