Looping through creating workbooks and saving them based on a range

MrJamesS

New Member
Joined
Oct 10, 2016
Messages
9
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

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
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sorry, just to add:

I've added Active.Workbook.Close for ease of asking the question above. Actually the macro continues past this and I need to reference this cell/file name in numerous places. e.g. If the cell in G5 is called "ABC", I need to filter by "ABC" in another part of the file. However I've unsure on how to reference it. Likewise I'll need to say Windows("ABC").Activate, but again not sure if this will work if my original query doesn't work.

Thanks
 
Upvote 0
Stupidly, I had a file open with the same name as the first cell in the range....:rolleyes:

Works now
 
Upvote 0
Solution

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top