SOLVED

Exit Sub not working - VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-1320772%22%20slang%3D%22en-US%22%3ERe%3A%20Exit%20Sub%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320772%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F143003%22%20target%3D%22_blank%22%3E%40Queenie%20Lai%3C%2FA%3E%26nbsp%3BI%20think%20you%20need%20this%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20copy()%0A%20%20%20%20Dim%20tabname%20As%20Name%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20closedBook%20%3D%20Workbooks.Open(%22C%3A%5Cfile123.xlsx%22)%0A%20%20%20%20For%20Each%20tabname%20In%20closedBook.Names%0A%20%20%20%20%20%20%20%20If%20tabname.Name%20%3D%20%22Apr2020%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Exit%20Sub%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%0A%20%20%20%20closedBook.Sheets(Sheets.Count).copy%20After%3A%3DclosedBook.Sheets(Sheets.Count)%0A%20%20%20%20closedBook.Close%20SaveChanges%3A%3DTrue%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1320406%22%20slang%3D%22en-US%22%3EExit%20Sub%20not%20working%20-%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1320406%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20looking%20to%20loop%20through%20the%20names%20of%20all%20sheets.%20If%20the%20name%22Apr2020%22%20exists%20then%20Exit%20Sub.%26nbsp%3BIf%20it%20doesn't%20exist%20then%20copy%20the%20last%20sheet%20after%20the%20last%20tab.%3C%2FP%3E%3CP%3EThe%20code%20below%20does%20not%20work%20and%20could%20anyone%20help%20to%20see%20what%20went%20wrong%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20for%26nbsp%3Btabname.Name%20%3D%20%22Apr2020%22%20%2C%20now%20I%20have%20to%20change%20the%20name%20in%20the%20code%20every%20month.%20How%20can%20I%20code%20it%20so%20it%20reflects%20the%20current%20month%20and%20year%20and%20I%20don't%20need%20to%20change%20the%20name%20every%20month%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ESub%20copy()%0A%0AApplication.ScreenUpdating%20%3D%20False%0A%0ASet%20closedBook%20%3D%20Workbooks.Open(%22C%3A%5Cfile123.xlsx%22)%0A%0A%0ADim%20tabname%20As%20Name%0AFor%20Each%20tabname%20In%20closedBook.Names%0AIf%20tabname.Name%20%3D%20%22Apr2020%22%20Then%0AExit%20Sub%0AElse%0AclosedBook.Sheets(Sheets.Count).copy%20After%3A%3DclosedBook.Sheets(Sheets.Count)%0AEnd%20If%0ANext%0A%0AclosedBook.Close%20SaveChanges%3A%3DTrue%0A%0A%0A%0AApplication.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1320406%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1323572%22%20slang%3D%22en-US%22%3ERe%3A%20Exit%20Sub%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1323572%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F22322%22%20target%3D%22_blank%22%3E%40Jan%20Karel%20Pieterse%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20another%20set%20of%20code%20from%20another%20forum.%20The%20code%20stops%20at%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3EActiveSheet.Name%20%3D%20strMonth%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20opened%20the%20Excel%20file%20and%20copied%20the%20last%20sheet%20and%20named%20Apr2020%20(2).%20Would%20you%20be%20able%20to%20look%20at%20this%20for%20me%20and%20fix%20the%20code%3F%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3ESub%20copy()%0A%20%20%20%20Dim%20wbkClosed%20As%20Workbook%0A%20%20%20%20Dim%20wksCurrent%20As%20Worksheet%0A%20%20%20%20Dim%20bolExists%20As%20Boolean%0A%20%20%20%20Dim%20strMonth%20As%20String%0A%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20closedBook%20%3D%20Workbooks.Open(%22C%3A%5CFile123.xlsx%22)%0A%20%20%20%20strMonth%20%3D%20Format(Now%2C%20%22Mmmyyyy%22)%0A%20%20%20%20bolExists%20%3D%20False%0A%20%20%20%20For%20Each%20tabname%20In%20closedBook.Names%0A%20%20%20%20%20%20%20%20If%20(closedBook.Name%20%3D%20strMonth)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20bolExists%20%3D%20True%0A%20%20%20%20%20%20%20%20%20%20%20%20Exit%20For%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%0A%20%20%20%20With%20wbkClosed%0A%20%20%20%20%20%20%20%20If%20bolExists%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20.Close%20SaveChanges%3A%3DFalse%0A%20%20%20%20%20%20%20%20%20%20%20%20MsgBox%20%22Worksheet%20'%22%20%26amp%3B%20strMonth%20%26amp%3B%20%22'%20not%20added%20(already%20exists)%22%2C%20vbOKOnly%20%2B%20vbInformation%2C%20%22Copy%22%0A%20%20%20%20%20%20%20%20Else%0A%20%20%20%20%20%20%20%20%20%20%20%20closedBook.Sheets(Sheets.Count).copy%20After%3A%3DclosedBook.Sheets(Sheets.Count)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20ActiveSheet.Name%20%3D%20strMonth%0A%20%20%20%20%20%20%20%20%20%20%20%20closedBook.Close%20SaveChanges%3A%3DTrue%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20MsgBox%20%22Worksheet%20'%22%20%26amp%3B%20strMonth%20%26amp%3B%20%22'%20added%22%2C%20vbOKOnly%20%2B%20vbInformation%2C%20%22Copy%22%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20End%20With%0A%20%20%20%20Set%20wksCurrent%20%3D%20Nothing%0A%20%20%20%20Set%20wbkClosed%20%3D%20Nothing%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1324111%22%20slang%3D%22en-US%22%3ERe%3A%20Exit%20Sub%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1324111%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F143003%22%20target%3D%22_blank%22%3E%40Queenie%20Lai%3C%2FA%3E%26nbsp%3BI%20am%20wondering%20whether%20the%20loop%20is%20correct.%20You%20are%20looping%20through%20the%20name%20collection%20of%20the%20workbook%20object.%20I%20*think*%20you%20intended%20to%20loop%20through%20the%20worksheet%20collection%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESub%20copy()%0A%20%20%20%20Dim%20tabname%20As%20Worksheet%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20closedBook%20%3D%20Workbooks.Open(%22C%3A%5Cfile123.xlsx%22)%0A%20%20%20%20For%20Each%20tabname%20In%20closedBook.Worksheets%0A%20%20%20%20%20%20%20%20If%20tabname.Name%20%3D%20%22Apr2020%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Exit%20Sub%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%0A%20%20%20%20closedBook.Sheets(Sheets.Count).copy%20After%3A%3DclosedBook.Sheets(Sheets.Count)%0A%20%20%20%20closedBook.Close%20SaveChanges%3A%3DTrue%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I am looking to loop through the names of all sheets. If the name"Apr2020" exists then Exit Sub. If it doesn't exist then copy the last sheet after the last tab.

The code below does not work and could anyone help to see what went wrong?

 

Also for tabname.Name = "Apr2020" , now I have to change the name in the code every month. How can I code it so it reflects the current month and year and I don't need to change the name every month? 

 

Sub copy()

Application.ScreenUpdating = False

Set closedBook = Workbooks.Open("C:\file123.xlsx")


Dim tabname As Name
For Each tabname In closedBook.Names
If tabname.Name = "Apr2020" Then
Exit Sub
Else
closedBook.Sheets(Sheets.Count).copy After:=closedBook.Sheets(Sheets.Count)
End If
Next

closedBook.Close SaveChanges:=True



Application.ScreenUpdating = True
End Sub

 

3 Replies
Highlighted

@Queenie Lai I think you need this:

Sub copy()
    Dim tabname As Name
    Application.ScreenUpdating = False
    Set closedBook = Workbooks.Open("C:\file123.xlsx")
    For Each tabname In closedBook.Names
        If tabname.Name = "Apr2020" Then
            Exit Sub
        End If
    Next
    closedBook.Sheets(Sheets.Count).copy After:=closedBook.Sheets(Sheets.Count)
    closedBook.Close SaveChanges:=True
    Application.ScreenUpdating = True
End Sub
Highlighted

@Jan Karel Pieterse 

 

I have another set of code from another forum. The code stops at 

 

ActiveSheet.Name = strMonth

 

It opened the Excel file and copied the last sheet and named Apr2020 (2). Would you be able to look at this for me and fix the code?

Sub copy()
    Dim wbkClosed As Workbook
    Dim wksCurrent As Worksheet
    Dim bolExists As Boolean
    Dim strMonth As String

    Application.ScreenUpdating = False
    Set closedBook = Workbooks.Open("C:\File123.xlsx")
    strMonth = Format(Now, "Mmmyyyy")
    bolExists = False
    For Each tabname In closedBook.Names
        If (closedBook.Name = strMonth) Then
            bolExists = True
            Exit For
        End If
    Next
    With wbkClosed
        If bolExists Then
            .Close SaveChanges:=False
            MsgBox "Worksheet '" & strMonth & "' not added (already exists)", vbOKOnly + vbInformation, "Copy"
        Else
            closedBook.Sheets(Sheets.Count).copy After:=closedBook.Sheets(Sheets.Count)

            ActiveSheet.Name = strMonth
            closedBook.Close SaveChanges:=True
            
            MsgBox "Worksheet '" & strMonth & "' added", vbOKOnly + vbInformation, "Copy"
        End If
    End With
    Set wksCurrent = Nothing
    Set wbkClosed = Nothing
    Application.ScreenUpdating = True
End Sub

 

Highlighted
Best Response confirmed by Queenie Lai (Occasional Contributor)
Solution

@Queenie Lai I am wondering whether the loop is correct. You are looping through the name collection of the workbook object. I *think* you intended to loop through the worksheet collection:

Sub copy()
    Dim tabname As Worksheet
    Application.ScreenUpdating = False
    Set closedBook = Workbooks.Open("C:\file123.xlsx")
    For Each tabname In closedBook.Worksheets
        If tabname.Name = "Apr2020" Then
            Exit Sub
        End If
    Next
    closedBook.Sheets(Sheets.Count).copy After:=closedBook.Sheets(Sheets.Count)
    closedBook.Close SaveChanges:=True
    Application.ScreenUpdating = True
End Sub