+ Reply to Thread
Results 1 to 3 of 3

Create new worksheets with names based on dropdown values

Hybrid View

  1. #1
    Registered User
    Join Date
    02-11-2014
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    19

    Create new worksheets with names based on dropdown values

    I've got a macro that autofilters my main sheet based on data entered into dropdowns (names) and checkboxes, and then creates new worksheets based on those criteria. (We're creating separate work orders for individual contractors who are named in the dropdowns.) It was working great until I was asked to make the sheets' names reflect the names selected in the dropdown menus, rather than their numerical values, and when I tried to implement that, it stopped working. Here's what I've got:

    Sub Test()
    
    Dim c As Range
    Dim i As Integer
    Dim Flag As Boolean
    
    For Each c In ActiveSheet.Range("I2:I10000")         'This is the column of linked cells, where the numerical value of each dropdown is stored.  The actual dropdowns are in column G.
    
        Dim dd As DropDown       'This section is what I added to try to pull the actual names from the dropdowns, rather than their numerical values
        For Each dd In ActiveSheet.DropDowns
            If dd.TopLeftCell.Column = 7 Then   'Only check dropdowns in column G
            myContractor = dd.List(dd.Value)
            End If
        Next dd
    
    
        Flag = True
        For i = 1 To Sheets().Count
            If Sheets(i).Name = "Work Order to " & myContractor Then Flag = False
        Next i
        
        If c.Value <> "" And c.Value <> Sheet1.[L2].Value And Flag Then
            Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Work Order to " & myContractor
            
            Sheet1.Shapes("Picture 5").Copy
            Sheets("Work Order to " & myContractor).[B2].PasteSpecial
            
            Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:=c.Value
            Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:=True
            
            Sheet1.[B11:B150].Copy
                With Sheets("Work Order to " & myContractor).[B11]
                    .PasteSpecial xlPasteColumnWidths
                    .PasteSpecial xlPasteValues
                End With
            Sheet1.[I10].AutoFilter
        
        ElseIf c.Value = Sheet1.[L2].Value And Flag Then
            Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Work Order to " & myContractor
            Sheet1.Shapes("Picture 5").Copy
            Sheets("Work Order to " & myContractor).[B2].PasteSpecial
            
            Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:=True
            Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:=c.Value
            
            Sheet1.[B11:B150].Copy
                With Sheets("Work Order to " & myContractor).[B11]
                    .PasteSpecial xlPasteColumnWidths
                    .PasteSpecial xlPasteValues
                End With
            Sheet1.[I10].AutoFilter
            
            Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:=True
            Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:="<>" & c.Value
            
            Sheet1.[B11:B150].Copy
                With Sheets("Work Order to " & myContractor).[B75]
                    .PasteSpecial xlPasteColumnWidths
                    .PasteSpecial xlPasteValues
                End With
            Sheet1.[I10].AutoFilter
            
            Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:="<>" & True
            Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:="<>" & ""
             Sheet1.[B11:B150].Copy
                With Sheets("Work Order to " & myContractor).[B150]
                    .PasteSpecial xlPasteColumnWidths
                    .PasteSpecial xlPasteValues
                End With
            Sheet1.[I10].AutoFilter
        
        End If
        
    Next c
    End Sub
    Previously, I was just naming the new sheets like so:
    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Work Order to " & c.Value
    Like I said, that was working great, but unfortunately the folks who tasked me with this project want the actual names on the sheets.

    I sort of understand why my current code isn't working, but can't quite work out how to get it running correctly.

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

    Re: Create new worksheets with names based on dropdown values

    I gave it my best guess. I may have botched it completely.

    Sub Test()
        
        Dim ShName As String
        Dim dd     As DropDown       'This section is what I added to try to pull the actual names from the dropdowns, rather than their numerical values
        
        For Each dd In ActiveSheet.DropDowns
            
            If dd.TopLeftCell.Column = 7 Then   'Only check dropdowns in column G
                
                If dd.Value > 0 Then  'Test if dropdown has a selection (same as value in column I)
                    
                    ShName = "Work Order to " & dd.List(dd.Value)    'Sheet Name
                    
                    'Test if sheet name doesn't already exist
                    If Not Evaluate("ISREF('" & ShName & "'!A1)") Then
                        '
                        Sheets.Add(After:=Sheets(Sheets.Count)).Name = ShName
                        
                        Sheet1.Shapes("Picture 5").Copy
                        Sheets(ShName).[B2].PasteSpecial
                        
                        Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:=dd.Value
                        Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:=True
                        
                        Sheet1.[B11:B150].Copy
                        With Sheets(ShName).[B11]
                            .PasteSpecial xlPasteColumnWidths
                            .PasteSpecial xlPasteValues
                        End With
                        Sheet1.[I10].AutoFilter
                        
                        If dd.Value = Sheet1.[L2].Value Then
                            
                            Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:=True
                            Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:="<>" & dd.Value
                            Sheet1.[B11:B150].Copy
                            With Sheets(ShName).[B75]
                                .PasteSpecial xlPasteColumnWidths
                                .PasteSpecial xlPasteValues
                            End With
                            Sheet1.[I10].AutoFilter
                            
                            Sheet1.[H10:I10000].AutoFilter Field:=1, Criteria1:="<>" & True
                            Sheet1.[H10:I10000].AutoFilter Field:=2, Criteria1:="<>" & ""
                            Sheet1.[B11:B150].Copy
                            With Sheets(ShName).[B150]
                                .PasteSpecial xlPasteColumnWidths
                                .PasteSpecial xlPasteValues
                            End With
                            Sheet1.[I10].AutoFilter
                            
                        End If
                    End If
                End If
            End If
        Next dd
        
    End Sub
    Last edited by AlphaFrog; 03-07-2014 at 09:10 PM.
    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.

  3. #3
    Registered User
    Join Date
    02-11-2014
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Create new worksheets with names based on dropdown values

    Working perfectly, 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. Replies: 2
    Last Post: 05-06-2013, 08:53 AM
  2. [SOLVED] Create multiple ranges based on an index of values that are also used as range names
    By Apog in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-05-2013, 12:48 PM
  3. [SOLVED] Macro to create new worksheets based on column values in a sheet
    By Arjen@bigpond in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2012, 11:18 PM
  4. Replies: 2
    Last Post: 02-06-2012, 07:39 PM
  5. Create Worksheets Based on Cell Values
    By modytrane in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2008, 04:55 PM

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