+ Reply to Thread
Results 1 to 3 of 3

VBA: Make a new sheet if it doesn't exist

  1. #1
    PaulW
    Guest

    VBA: Make a new sheet if it doesn't exist

    Since my last post is pages behind, thought I'd repost to gain further help.
    Got this from my initial query (cheers Don Guillett)

    Sub mynewsheets()
    For Each c In Range("myrange")
    On Error Resume Next
    If Sheets.Name <> c Then
    Sheets.Add.Name = c
    End If
    Next c
    End Sub

    I changed myrange to Range("B2:B8") where i've put some names. When I run
    the macro I get "Compile Error: Method or data member not found" and .Name is
    highlighted in the If Sheets.Name <> c Then row.
    Am I not loading something I should be?

    Can anyone help?

  2. #2
    Jon Peltier
    Guest

    Re: Make a new sheet if it doesn't exist

    There seems to be a line or two missing. Try this:

    Sub mynewsheets()
    Dim c As Range
    Dim ws As Worksheet
    For Each c In Range("myrange").Cells
    Set ws = Nothing
    On Error Resume Next
    Set ws = Worksheets(c.Value)
    On Error GoTo 0
    If ws Is Nothing Then
    Worksheets.Add.Name = c.Value
    End If
    Next c
    End Sub

    - Jon
    -------
    Jon Peltier, Microsoft Excel MVP
    Peltier Technical Services
    Tutorials and Custom Solutions
    http://PeltierTech.com/
    _______


    "PaulW" <PaulW@discussions.microsoft.com> wrote in message
    news:9CD560EF-4050-424F-A8A5-3852B9E933C7@microsoft.com...
    > Since my last post is pages behind, thought I'd repost to gain further
    > help.
    > Got this from my initial query (cheers Don Guillett)
    >
    > Sub mynewsheets()
    > For Each c In Range("myrange")
    > On Error Resume Next
    > If Sheets.Name <> c Then
    > Sheets.Add.Name = c
    > End If
    > Next c
    > End Sub
    >
    > I changed myrange to Range("B2:B8") where i've put some names. When I run
    > the macro I get "Compile Error: Method or data member not found" and .Name
    > is
    > highlighted in the If Sheets.Name <> c Then row.
    > Am I not loading something I should be?
    >
    > Can anyone help?




  3. #3
    PaulW
    Guest

    Re: Make a new sheet if it doesn't exist

    Cheers, this is working great!

    "Jon Peltier" wrote:

    > There seems to be a line or two missing. Try this:
    >
    > Sub mynewsheets()
    > Dim c As Range
    > Dim ws As Worksheet
    > For Each c In Range("myrange").Cells
    > Set ws = Nothing
    > On Error Resume Next
    > Set ws = Worksheets(c.Value)
    > On Error GoTo 0
    > If ws Is Nothing Then
    > Worksheets.Add.Name = c.Value
    > End If
    > Next c
    > End Sub
    >
    > - Jon
    > -------
    > Jon Peltier, Microsoft Excel MVP
    > Peltier Technical Services
    > Tutorials and Custom Solutions
    > http://PeltierTech.com/
    > _______
    >
    >
    > "PaulW" <PaulW@discussions.microsoft.com> wrote in message
    > news:9CD560EF-4050-424F-A8A5-3852B9E933C7@microsoft.com...
    > > Since my last post is pages behind, thought I'd repost to gain further
    > > help.
    > > Got this from my initial query (cheers Don Guillett)
    > >
    > > Sub mynewsheets()
    > > For Each c In Range("myrange")
    > > On Error Resume Next
    > > If Sheets.Name <> c Then
    > > Sheets.Add.Name = c
    > > End If
    > > Next c
    > > End Sub
    > >
    > > I changed myrange to Range("B2:B8") where i've put some names. When I run
    > > the macro I get "Compile Error: Method or data member not found" and .Name
    > > is
    > > highlighted in the If Sheets.Name <> c Then row.
    > > Am I not loading something I should be?
    > >
    > > Can anyone help?

    >
    >
    >


+ 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