Hi,
Am a basic VBA user, so forgive me for blatent errors
I am trying to do the following:
- "control" sheet shows filepath location (d5) as well as a range of names (g5:g7)
- in filepath, if folder "Club Data Files" does not exist, create it
- in new folder, create workbook and save it based on the 1st cell in a range, and close workbook
- loop through creating a workbook based on the next cell in the range, save, close, and so on
I have reduced the range for ease of testing. I am getting an error when it's trying to save the new workbook, possibly because of how it's interpreting the cell/clubname? Trial and error isn't working
Am a basic VBA user, so forgive me for blatent errors
I am trying to do the following:
- "control" sheet shows filepath location (d5) as well as a range of names (g5:g7)
- in filepath, if folder "Club Data Files" does not exist, create it
- in new folder, create workbook and save it based on the 1st cell in a range, and close workbook
- loop through creating a workbook based on the next cell in the range, save, close, and so on
I have reduced the range for ease of testing. I am getting an error when it's trying to save the new workbook, possibly because of how it's interpreting the cell/clubname? Trial and error isn't working
VBA Code:
Sub Test()
'
' Test Macro
'
' Go to Control sheet
Sheets("Control").Select
' If club data folder does not exist, create it. If it does, continue
Dim Filelocation As String
Filelocation = Range("d5")
Dim fdObj As Object
Application.ScreenUpdating = False
Set fdObj = CreateObject("Scripting.FileSystemObject")
If fdObj.FolderExists(Filelocation & "Club Data Files") Then
Else
fdObj.CreateFolder (Filelocation & "Club Data Files")
End If
' Calculate
Calculate
' Loop through clubs - set club range
Dim clubs As Range
Dim cell As Range
Dim clubname As String
Set clubs = Sheets("Control").Range("g5:g7")
For Each cell In clubs
clubname = cell.Value
' Create new file and save as club name
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=Filelocation & "Club Data Files\" & clubname & ".xlsx"
Active.Workbook.Close
'Loop through
Next cell
Application.ScreenUpdating = True
End Sub