VBA keeps closing BOTH workbooks (only 1 specified)

jamesha777

New Member
Joined
Jul 15, 2019
Messages
2
Hi all - any help would be appreciated.

I have VBA that loops through a tab on a 'master sheet', changing the club on the tab, and saves down as a separate file for that club. It repeats this for each club, saving the sheet and closing it down each time to return to the 'master sheet'.

I have two master sheets that perform different functions, but all in all, just have different formula. If I change the vba to reference this other master sheet, after saving the first club's file down, it closes all Excel sheets open, and so obviously breaks the code.

Does anyone have any answers? I've tried using both Activeworkbook.Close & Workbooks("").Close to specify, but both end up closing excel entirely.
It's really odd as the VBA works when referencing he first master sheet.
Code:
Sub CreateFiles()
'
    Dim mySheetName As String
    Dim numberClubs As Integer
    Dim myClubName As String
    Dim myClubCode As String
    Dim mytab As String
    Dim fileLocation As String
    
    'Sets file location to save club down later
    fileLocation = Sheets("LookUps").Range("I2")
    
    'sets which master sheet to use for looping clubs
    mytab = Sheets("LookUps").Range("G2")
    
    'How many clubs to loop through
    numberClubs = Sheets("Lookups").Range("E2")
    
    
    'Loop clubs through master sheets and save as individual files
    For clubCounter = 2 To numberClubs + 1
    
    
        'store club code & club name as string
        myClubName = Sheets("LookUps").Range("B" & clubCounter)
        myClubCode = "'" & Sheets("LookUps").Range("a" & clubCounter).Text


        'enter the club code in the master sheet
        Sheets("2020CombinedTarget").Range("C3") = myClubCode
        
    
    'copy as a new sheet and save as values
    Sheets(mytab).Select
    Sheets(mytab).Copy after:=Sheets(mytab)
    Cells.Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
    'Change sheet name to club name
    mySheetName = "T" & Right(myClubCode, 3)
    ActiveSheet.Name = mySheetName


    'Save down file
    Sheets(mySheetName).Move
    ActiveWorkbook.SaveAs Filename:= _
        fileLocation & mySheetName & ".xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False

'I suspect this is the problem area?????
    Workbooks(mySheetName & ".xlsx").Close


    Next
    
End Sub
 
Last edited by a moderator:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
From further research, it looks as if 'Workbooks (").Close can cause glitches in Excel, closing all open workbooks. Does anyone have a work around for this? I'm desperate now for work!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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