+ Reply to Thread
Results 1 to 7 of 7

Error Worksheet Exists - need code to continue to next name if it does

Hybrid View

missit Error Worksheet Exists - need... 03-28-2014, 05:49 PM
moinsk21 Re: Error Worksheet Exists -... 03-28-2014, 06:01 PM
missit Re: Error Worksheet Exists -... 03-28-2014, 06:10 PM
AlphaFrog Re: Error Worksheet Exists -... 03-28-2014, 09:28 PM
missit Re: Error Worksheet Exists -... 03-28-2014, 11:22 PM
AlphaFrog Re: Error Worksheet Exists -... 03-29-2014, 12:01 AM
missit Re: Error Worksheet Exists -... 03-29-2014, 12:10 AM
  1. #1
    Registered User
    Join Date
    10-13-2010
    Location
    Colorado Springs, CO
    MS-Off Ver
    Excel 2010
    Posts
    55

    Error Worksheet Exists - need code to continue to next name if it does

    Hi,

    I have the following code that is used to generate worksheets automatically from a listing of names on the "Master Provider List". It works perfectly, but I have found that sometimes I need to add more names to the list. I need the code to look at the list and if the worksheet related to the name already exists, continue on to the next name and create the worksheet. I have never trapped an error before, so not sure how to add that to my code.
    Private Sub Create_PList_Click()
    'creates the provider's sheets, names the sheet, and enters sheet name in c4 of the new sheet
    Dim i As Integer
        Dim ws As Worksheet
        Dim Sh As Worksheet
        Set ws = Sheets("Audit Temp")
        Set Sh = Sheets("Master Provider List")
        
        Application.ScreenUpdating = 0
        Sheets("Audit Temp").Visible = True
        For i = 3 To Range("A" & Rows.Count).End(xlUp).Row
            Sheets("Audit Temp").Copy After:=ws
            ActiveSheet.Name = Sh.Range("A" & i).Value
            ActiveSheet.Cells(4, 3) = Sh.Cells(i, 1)
        Next i
        Sheets("Audit Temp").Visible = False
        Sheets("Master Provider List").Activate
    
    End Sub

    Thanks so much in advance

    C
    Last edited by missit; 03-28-2014 at 06:08 PM. Reason: Fixed so code shows correctly.

  2. #2
    Registered User
    Join Date
    03-19-2014
    Location
    India
    MS-Off Ver
    Excel & VBA - 2007 & 2010
    Posts
    60

    Re: Error Worksheet Exists - need code to continue to next name if it does

    Hi,

    First of all, please input the code in the selection box.

    And You can try
    On Error Resume Next
    for this

    Thanks!
    Moinuddin Shaikh

    If you like my Response:
    1. Show appreciation to those who have helped you by clicking below their posts.
    2. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    Appreciate someone towards their career path is better than saying Thanks!
    -Moin

  3. #3
    Registered User
    Join Date
    10-13-2010
    Location
    Colorado Springs, CO
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Error Worksheet Exists - need code to continue to next name if it does

    thanks for the quick response, where to I put that?

    I can figure out how to use code, but not good at writing it or adding to it. Still novice in that area.



    C

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Error Worksheet Exists - need code to continue to next name if it does

    Using On Error Resume Next would ignore worksheet naming errors, but it would also leave you with un-named copies of your template worksheet.

    Try this...

    Private Sub Create_PList_Click()
    'creates the provider's sheets, names the sheet, and enters sheet name in c4 of the new sheet
        Dim cell As Range
        Dim wsAT As Worksheet
        Dim wsMPL As Worksheet
        
        Set wsAT = Sheets("Audit Temp")
        Set wsMPL = Sheets("Master Provider List")
        
        Application.ScreenUpdating = False
        
        For Each cell In wsMPL.Range("A3", wsMPL.Range("A" & Rows.Count).End(xlUp))
            If Not Evaluate("ISREF('" & cell.Value & "'!A1)") Then  'Test if worksheet name exists
                wsAT.Copy After:=wsAT
                ActiveSheet.Visible = True
                ActiveSheet.Name = cell.Value
                ActiveSheet.Cells(4, 3) = cell.Value
            End If
        Next cell
        
        Sheets("Master Provider List").Activate
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    10-13-2010
    Location
    Colorado Springs, CO
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Error Worksheet Exists - need code to continue to next name if it does

    Hi,

    When I try this code, it adds an additional copy of the Audittemp worksheet and changes the name of the existing worksheet. Thanks for trying, but what I need is to have a listing of names on Master Provider List and have it automatically copy the AuditTemp and rename that sheet with one of the names on the Master Provider List. thanks for the help.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Error Worksheet Exists - need code to continue to next name if it does

    It worlked for me.

    Try this...

    Private Sub Create_PList_Click()
    'creates the provider's sheets, names the sheet, and enters sheet name in c4 of the new sheet
        Dim cell As Range
        Dim wsAT As Worksheet
        Dim wsMPL As Worksheet
        
        Set wsAT = Sheets("Audit Temp")
        Set wsMPL = Sheets("Master Provider List")
        
        Application.ScreenUpdating = False
        
        wsAT.Visible = xlSheetVisible
        For Each cell In wsMPL.Range("A3", wsMPL.Range("A" & Rows.Count).End(xlUp))
            If Not Evaluate("ISREF('" & cell.Value & "'!A1)") Then  'Test if worksheet name exists
                wsAT.Copy After:=wsAT
                ActiveSheet.Visible = True
                ActiveSheet.Name = cell.Value
                ActiveSheet.Cells(4, 3) = cell.Value
            End If
        Next cell
        wsAT.Visible = xlSheetHidden
        Sheets("Master Provider List").Activate
        Application.ScreenUpdating = True
        
    End Sub

  7. #7
    Registered User
    Join Date
    10-13-2010
    Location
    Colorado Springs, CO
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Error Worksheet Exists - need code to continue to next name if it does

    Hi,

    That worked !!!! Thank you so much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro code to continue iterating if error.
    By sid9221 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-23-2013, 03:39 PM
  2. [SOLVED] IF Exists or on Error Then - Skip Code- How to write code?
    By Jack7774 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-22-2013, 06:45 PM
  3. [SOLVED] changing cell on active sheet does not cause code to continue - No error codes either
    By SoteriaLive in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-01-2012, 11:21 AM
  4. VBA Code to continue in Update Links error message
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-21-2011, 03:53 PM
  5. Code error, highlight error, continue to RUN
    By JohnSeito in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-07-2008, 01:29 AM

Tags for this Thread

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