will2learn
Board Regular
- Joined
- Dec 1, 2005
- Messages
- 144
I'm using some code I found on this site to select the main folder and save a file to it. I now need to amend the code so that it saves the file into a subfolder which is named in a cell.
The cell where the folder name is loacted is in a sheet called LookUpLists cell N9 and this is a lookup formula.
Basically, what I'm trying to do is once I selected the main folder and the macro is going through each record, I want the new file to saved into the folder and into the subfolder named in cell N9. If the folder is not there then I need it to be created and add the file, if it does exist then simply add the file to it.
I have to create over a thousand files into specific folders.
Can anyone help?
The cell where the folder name is loacted is in a sheet called LookUpLists cell N9 and this is a lookup formula.
Basically, what I'm trying to do is once I selected the main folder and the macro is going through each record, I want the new file to saved into the folder and into the subfolder named in cell N9. If the folder is not there then I need it to be created and add the file, if it does exist then simply add the file to it.
I have to create over a thousand files into specific folders.
Can anyone help?
Code:
sub MyMacro()
Dim sFilter As String
Dim sPath As String
UserFile = PickFolder(strStartDir)
sPath = UserFile & "\" 'ActiveWorkbook.Path & "\"
For Each c In Range("J2:J" & Range("J65536").End(xlUp).Row)
sFilter = c.Value
Application.StatusBar = "File for " & c.Value & " is currently being created!"
With Sheets("LookupLists").Range("N2")
.Value = c.Value
End With
Set r = Worksheets("LookupLists").Range("O14", Worksheets("LookupLists").Range("O" & Rows.Count).End(xlUp))
Set ws = Sheets(WorksheetFunction.Transpose(r))
ws.Select
ws.Copy
ActiveWorkbook.SaveAs Filename:=sPath & sFilter & Format(Date, " YYYY MM DD ") & Format(Time, "hh mm") & ".xls"
ActiveWindow.Close
Next c
End Sub
Option Explicit
Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, F As Object
Set SA = CreateObject("Shell.application")
Set F = SA.BrowseForFolder(0, "Choose a folder", 0, strStartDir)
If (Not F Is Nothing) Then
PickFolder = F.items.Item.Path
End If
Set F = Nothing
Set SA = Nothing
End Function
Last edited by a moderator: