+ Reply to Thread
Results 1 to 8 of 8

Need a VB code to generate sheets from a list of cells

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2008
    Location
    Blackpool
    Posts
    22

    Need a VB code to generate sheets from a list of cells

    Hi, first post here, but have already used the site many times to help me compile a few spreadsheets for work...

    i am pretty poor with excel in comparrison to you guys, but am getting there slowly but surely

    I used this code from another thread, its used to generate worksheets with a specific name from a list...

    Sub copysht()
        Dim cl     As Range
        For Each cl In Sheet1.Range("A:A")
            Sheet2.Copy after:=Sheets(Worksheets.Count)
            ActiveSheet.Name = cl.Value
        Next cl
    End Sub
    So at present, its copying sheet 2 and naming them from sheet 1 column A, but i need two things additionally which i am not sure how to do...

    firstly, i need it to automatically stop when it runs out of names in the column A rather than generate an error when the list runs out.

    Secondly, i need it to operate in a "live" enviorment, so the second someone adds to the list it creates a new worksheet with the corresponding name, and also when a name on the list is deleted it deletes the corresponding worksheet.

    Any help much appreciated as its way beyond my capabilities!
    Last edited by dominicb; 08-06-2008 at 09:31 AM.

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon Thanatos

    ...and welcome to the forum!!

    Please try and remember in future to wrap your code using the "'#" button from the toolbar above - I've done it for you this time.

    Have a look at the file attached. What you're asking is not that straightforward as it requires two checks comparing the range to the sheet names before doing any actions - and one of these requires a loop within a loop. As you want this doing as the user is typing, this may slow the file down a little, and also your undo stack will constantly be cleared (ie you lose your undo functionality on this file) - this is unavoidable. What I've done is a first bash and seems to work OK as a starting point.

    HTH

    DominicB
    Attached Files Attached Files
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Forum Contributor
    Join Date
    06-03-2008
    Posts
    387
    This should work for the most part. Paste it into a workbook, save, and reopen. Then type anywhere in column A in worksheet 1. Note: Column B will need to be used as well. This code needs to keep track of the name the last time it did an update, so that when you delete a file it knows what file to delete and also if you make a name change.

    Sub auto_open()
    
       ' Run the macro DidCellsChange any time a entry is made in a
       ' cell in Sheet1.
       ThisWorkbook.Worksheets("Sheet1").OnEntry = "DidCellsChange"
    
    End Sub
    
    
    Sub DidCellsChange()
      Dim KeyCells As String
       ' Define which cells should trigger the KeyCellsChanged macro.
       KeyCells = "A1:A100"
    
       ' If the Activecell is one of the key cells, call the
       ' KeyCellsChanged macro.
       If Not Application.Intersect(ActiveCell, Range(KeyCells)) _
       Is Nothing Then copysht
    
    End Sub
    
    
    Sub copysht()
        Dim cl     As Range
        Col = 1
        Row = 1
        EmptySheets = 0
        Do
            If IsEmpty(Sheet1.Cells(Row, Col).Value) Then
                If IsEmpty(Sheet1.Cells(Row, Col + 1).Value) Then
                Else
                    Application.DisplayAlerts = False
                    Worksheets(Sheet1.Cells(Row, Col + 1).Value).Delete
                    Application.DisplayAlerts = True
                    Sheet1.Cells(Row, Col + 1).ClearContents
                End If
                EmptySheets = EmptySheets + 1
            ElseIf IsEmpty(Sheet1.Cells(Row, Col + 1)) Then
                Sheet2.Copy after:=Sheets(Row + 1 - EmptySheets)
                ActiveSheet.Name = Sheet1.Cells(Row, Col).Value
                Sheet1.Cells(Row, Col + 1) = Sheet1.Cells(Row, Col)
            Else
                If Sheet1.Cells(Row, Col + 1).Value = Sheet1.Cells(Row, Col).Value Then
                    'Nothing Changed
                Else
                    Worksheets(Sheet1.Cells(Row, Col + 1).Value).Select
                    ActiveSheet.Name = Sheet1.Cells(Row, Col).Value
                    Sheet1.Cells(Row, Col + 1) = Sheet1.Cells(Row, Col)
                End If
            End If
            Row = Row + 1
        Loop Until Row >= 50
        Sheet1.Select
    End Sub

  4. #4
    Registered User
    Join Date
    08-04-2008
    Location
    Blackpool
    Posts
    22
    Heading off home in 5 mins, so will try these out tomorrow - thanks guys, looks like you may have cracked in 20 minutes what took me all day! rofl


    Think i am gonna like this forum!

  5. #5
    Registered User
    Join Date
    08-04-2008
    Location
    Blackpool
    Posts
    22
    Quote Originally Posted by dominicb
    Good afternoon Thanatos

    Have a look at the file attached. What you're asking is not that straightforward as it requires two checks comparing the range to the sheet names before doing any actions - and one of these requires a loop within a loop. As you want this doing as the user is typing, this may slow the file down a little, and also your undo stack will constantly be cleared (ie you lose your undo functionality on this file) - this is unavoidable. What I've done is a first bash and seems to work OK as a starting point.

    HTH
    DominicB
    Your code worked a treat (couldnt get yawnzzz to work ). However I have a couple of tweaks i need adding to it, wondered if you woud know how...

    I have some sheets i want to add manually that wont be affected by the code, and I need the replicated sheets to be copied from sheet 2...

    Might be easier to explain what I am doing... lol

    Essentially i am creating a document to monitor employees case loads (what they achieve daily.

    The idea behind this part of the code I am looking for is that each staff member has their own sheet (only they can see - I have perfected that code already). The master user - e.g. the manager has access to all sheets, one of which has a list of staff numbers which will create the worksheets the employees have access to. So essentially when a new guy starts it adds a sheet for them and when one leaves it removes it. It's the only way I can think of that a lay user can maintain the workbook even with no excel knowledge...

    Does that help explain? So essentially employee sheets need auto creating (copying from existing sheet) and the rest of the sheets need leaving alone.


    Sorry to be a pain guys!!

    edit: Also just noticed it doesnt recognise numbers and create new sheets from them. letters only... that would be a problem too.
    Last edited by Thanatos; 08-07-2008 at 04:01 AM.

  6. #6
    Registered User
    Join Date
    08-04-2008
    Location
    Blackpool
    Posts
    22
    Have sorted two aspects of above problems....

    If it format the cells containing numbers as text it reads them, so thats no issues now.

    And to ensure it wont delete the worksheets i dont want touching i can simply add them to the list in column A and hide the rows. So thats sorted too...

    All that remains is for it to copy an existing sheet rather than create a new one.

    Is it possible to add the new sheets before a sheet called "end" rather than automatticaly placing them as the last sheet in the workbook?


    Thanks in advance for any ideas!

  7. #7
    Registered User
    Join Date
    08-04-2008
    Location
    Blackpool
    Posts
    22
    Quote Originally Posted by Thanatos

    Is it possible to add the new sheets before a sheet called "end" rather than automatticaly placing them as the last sheet in the workbook?

    Thanks in advance for any ideas!
    Fixed above with this replacing 7th line of code
    ActiveSheet.Move Before:=Sheets(“end”)
    All that remains is for it to copy an existing sheet rather than create a new one

    Anyone up for the challenge? :D

    Current code read:


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    On Error Resume Next
    a = Sheets("adduser").Range("A65536").End(xlUp).Row
    For n = 1 To a
    If SheetExists(Sheets("adduser").Cells(n, 1).Value) = False Then
    Sheets.Add.Name = Sheets("adduser").Cells(n, 1).Value
    ActiveSheet.Move Before:=Sheets("end")
    End If
    Next n
    For Each WkSht In ActiveWorkbook.Worksheets
    chk = 0
    For n = 1 To a
    If WkSht.Name = Sheets("adduser").Cells(n, 1).Value Then chk = 1
    Next n
    If chk = 0 And Not WkSht.Name = "adduser" Then
    Sheets(WkSht.Name).Delete
    End If
    Next WkSht
    Sheets("adduser").Activate
    End Sub
    
    Private Function SheetExists(Sht) As Boolean
    On Error Resume Next
    Set x = ActiveWorkbook.Sheets(Sht)
    If Err = 0 Then
    SheetExists = True
    Else
    SheetExists = False
    End If
    End Function

  8. #8
    Registered User
    Join Date
    08-04-2008
    Location
    Blackpool
    Posts
    22
    Managed to do it by combining the two codes you guys supplied. Doubt it will help anyone here, but this is the final code:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    ActiveWorkbook.Unprotect "letmein"
    On Error Resume Next
    a = Sheets("adduser").Range("A65536").End(xlUp).Row
    For n = 1 To a
    If SheetExists(Sheets("adduser").Cells(n, 1).Value) = False Then
    Sheet3.Copy Before:=Sheets("end")
    ActiveSheet.Name = Sheets("adduser").Cells(n, 1).Value
    End If
    Next n
    For Each WkSht In ActiveWorkbook.Worksheets
    chk = 0
    For n = 1 To a
    If WkSht.Name = Sheets("adduser").Cells(n, 1).Value Then chk = 1
    Next n
    If chk = 0 And Not WkSht.Name = "adduser" Then
    Sheets(WkSht.Name).Delete
    End If
    Next WkSht
    Sheets("adduser").Activate
    End Sub
    
    Private Function SheetExists(Sht) As Boolean
    On Error Resume Next
    Set x = ActiveWorkbook.Sheets(Sht)
    If Err = 0 Then
    SheetExists = True
    Else
    SheetExists = False
    End If
    End Function

    Thanks for all your 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