+ Reply to Thread
Results 1 to 2 of 2

Trying to determine if worksheet exists, subscript out of range error

Hybrid View

  1. #1
    Registered User
    Join Date
    09-26-2007
    Posts
    26

    Trying to determine if worksheet exists, subscript out of range error

    Hello,

    Here is what I need help with:

    The app I'm working on is used quarterly. For this procedure I want to check the current file and see if the worksheet 'Proposed' exists. If it does exist, then I want to delete it. If it doesn't exist, I want to add it. Eventually (not in this sub) I want to open up another excel file that the user has designated and then copy the values from that file into the worksheet that I have created.

    For some reason or another, the below code is choking. I keep getting a Run-Time Error '9': Subscript out of range. It chokes on the first line Set WsA = Sheets("Proposed").

    Any help would be greatly appreciated.

    -js999

    
    Sub Does_Proposed_Ws_Exist()
    
        Dim wsA As Worksheet
        
        Set wsA = Sheets("Proposed")    
        'Find worksheet
        If wsA Is Nothing Then
            'Proposed doesn't exist
            MsgBox "Worksheet does not exist."
            Set wsA = Nothing
            
            'Insert into Report file sheet named "Proposed"
            Sheets.Add.Name = "Proposed"
        Else
            'Ws exists, lets delete it and insert a new one to have a 'clean' file
            Set wsA = Nothing
            
            ActiveWorkbook.Sheets("Proposed").Activate
            
            Application.DisplayAlerts = False       'Disable alert when deleting wksheet
            For Each wsA In ThisWorkbook.Worksheets
                If wsA.Name = "Proposed" Then
                    wsA.Delete
                End If
            Next
            Application.DisplayAlerts = True        'Enable alerts again
            Sheets.Add.Name = "Proposed"
        End If
    
    End Sub

  2. #2
    Registered User
    Join Date
    09-26-2007
    Posts
    26
    Boy ... too much coffee this morning ...
    Here is the solution ... apologies for wasting the server space.

    -js999

    
    Sub Does_Proposed_Ws_Exist()
    
        Dim wsA As Worksheet
        
        Set wsA = Nothing
        
        Application.DisplayAlerts = False       'Disable alert when deleting wksheet
        For Each wsA In ThisWorkbook.Worksheets
            If wsA.Name = "Proposed" Then
                wsA.Delete
            End If
        Next
        
        Application.DisplayAlerts = True        'Enable alerts again
        Sheets.Add.Name = "Proposed"
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1