Consulting

Results 1 to 5 of 5

Thread: Need to test if Worksheet Name Exists

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    19
    Location

    Need to test if Worksheet Name Exists

    I have a report and I want to parse the data into seperate Worksheets By Hospital Name. Since the Hospital Name can exist multiple times in the data I need to loop thru the worksheets before creating a new worksheet to make sure the name doesn't already exist.

    I can't Sort the file to group all hospital names together it has to stay in the order it is in....

    I am attaching some sample data

    Below is the code I am trying to use to Loop thru the data and create one worksheet for each Hospital Name:


     
    Dim rngstartP As Range
    Dim rngendP As Range
    Dim BExists As Boolean
     
     
    BExists = False
    For ct = 1 To Worksheets.Count
            If rngstartP.Value = Worksheets(ct).Name Then
                BExists = True
                Exit For
            End If
    Next ct
            If BExists = False Then
     
    For Each rngstartP In rngendP
            If rngstartP.Value <> rngstartP.Offset(-1, 0).Value Then
               Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = rngstartP.Offset(1, 0)
            End If
    Next rngstartP
     
     
     
     
    End If
    End Sub

    Any help is greatly appreciated

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Untested

    [vba]

    Dim rngstartP As Range
    Dim rngendP As Range
    Dim arySheets As Variant


    ReDim arySheets(1 To Worksheets.Count)
    For ct = 1 To Worksheets.Count

    arySheets(i) = Worksheets(i).Name
    Next ct

    For Each rngstartP In rngendP
    If IsError(Application.Match(rngstartP.Value, arySheets, 0)) Then

    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = rngstartP.Offset(1, 0).Value
    ReDim Preserve arySheets(1 To UBound(arySheets) + 1)
    arySheets(UBound(arySheets)) = rngstartP.Offset(1, 0).Value
    End If
    Next rngstartP
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    19
    Location
    I tested the code and I get an 'Subscript Out of Range Error' and it highlights the following code arySheets(i) = Worksheets(i).Name

    I am pretty new to VBA so I apologize......

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Got my loop counters mixed

    [vba]

    ReDim arySheets(1 To Worksheets.Count)
    For ct = 1 To Worksheets.Count

    arySheets(ct) = Worksheets(ct).Name
    Next ct

    For Each rngstartP In rngendP
    If IsError(Application.Match(rngstartP.Value, arySheets, 0)) Then

    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = rngstartP.Offset(1, 0).Value
    ReDim Preserve arySheets(1 To UBound(arySheets) + 1)
    arySheets(UBound(arySheets)) = rngstartP.Offset(1, 0).Value
    End If
    Next rngstartP
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Apr 2009
    Posts
    19
    Location
    Worked like a Charm! I appreciate your help!

Posting Permissions

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