Hello everyone. First post, and verrrry new to VBA/macros, so please be gentle. I've been searching the forums and can't seem to find the answer I'm looking for. What I have found isn't working when I try to add it.
I appropriated a bit of code which provides an input box for the date and saves the file as that was kindly supplied to another user by Leith Ross in this thread: http://www.excelforum.com/excel-prog...e-file-as.html. I made some tiny changes -- text in the input box, file path, that kind of thing) and ended up with:
Sub Button26_Click()
Dim InputDate As Variant
Dim FileType As String
Dim myFileName As String
Dim myFolder As String
FileType = ".xls"
myFileName = "Service Desk Report: "
myFolder = "J:\Library Stats\Service desk"
EnterDate:
InputDate = InputBox("Please enter date of Report mm/dd/yy")
If InputDate = "" Then Exit Sub
If Not IsDate(InputDate) Then
MsgBox "The date you entered is not valid"
GoTo EnterDate
End If
InputDate = " " & Replace(InputDate, "/", "-")
ThisWorkbook.SaveAs myFolder & "\" & myFileName & InputDate & FileType
End Sub
This works perfectly as it is. What I would like to do, though, is send the file to a folder corresponding to the current month. Searching, I saw mkDir as an option, but I don't want new folders each time I save (all May worksheets should go in one May folder, &c) and I didn't know how to incorporate mkDir anyway, so I thought pre-creating the folders and adding a series of If statements would work. I wound up with something like this:
Sub Button26_Click()
Dim InputDate As Variant
Dim FileType As String
Dim myFileName As String
Dim myFolder As String
If Worksheets("input sheet").Range("a71").value = 5 then
FileType = ".xls"
myFileName = "Service Desk Report: "
myFolder = "J:\Library Stats\Service desk\May"
else
If Worksheets("input sheet").Range("A71").Value = 6 then
FileType = ".xls"
myFileName = "Service Desk Report: "
myFolder = "J:\Library Stats\Service desk\June"
End If
End if
And so forth, with a statement for each month.
When I run this, I get the input box, and it lets me enter the date, but then I get Error 1004, saying it can't find path C:\105264 (the number is made up; i reverted to the IF-less code) and to make sure it exists or isn't being used by another program. When I click debug, the line highlighted is:
ThisWorkbook.SaveAs myFolder & "\" & myFileName & InputDate & FileType
I don't see why adding the IFs should make a difference there, or what the problem is. Obviously there's something I'm not getting. If anyone could point me in the right direction, I'd be endlessly grateful.
-HD
Bookmarks