VBA Code for Dropdown List with Assigned Macros

benri

New Member
Joined
Jun 12, 2018
Messages
16
Hi everyone,

Long time forum searcher, first time posting.

I am hoping to get some direction with code that has completely stumped me. I have written code that is pretty basic, and the end result is a workbook with a number of columns with thousands of rows of data. Currently the code creates three worksheets in the same workbook to view the data different ways (via AutoFilter), but I'd much rather have a single worksheet with a dropdown list so a user could select a few ways to view the data. I have scoured the web looking for a solution, but it eludes me.

I was able to write code that creates the dropdown list with the three filter views, but no action is taken when selecting any of the drop down items.

The code I am posting below is the second set of procedures. I am very new to VBA, so please be gentle.

Thanks to everyone in advance for taking the time to read this.

Code:
Option Explicit


Sub LocTrackerMacro2()


' localization tracker macro2, second set of procedures ; module 3


    Dim Output As Workbook, Source As Workbook
    Dim sh As Worksheet
    Dim FileName As String
    Dim firstcell
    
    Application.ScreenUpdating = False
    Set Source = ActiveWorkbook
    
    Set Output = Workbooks.Add
    Application.DisplayAlerts = False
    
    Dim i As Integer
    
    For Each sh In Source.Worksheets
    
        Dim NewSheet As Worksheet
        
        ' select all used cells in the source sheet
        sh.Activate
        sh.UsedRange.Select
        Application.CutCopyMode = False
        Selection.Copy
        
        ' create new destination sheet
        Set NewSheet = Output.Worksheets.Add
        NewSheet.Name = "AllEvents"
        
        ' make sure the destination sheet is selected with right cell
        NewSheet.Activate
        firstcell = sh.UsedRange.Cells(1, 1).Address
        NewSheet.Range(firstcell).Select
        
        ' paste the values
        Range(firstcell).PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, SkipBlanks:=True, Transpose:=False
        
        ' delete the empty sheet in the new workbook
        Worksheets("Sheet1").Delete
        
        ' turn on autofilter
        If ActiveSheet.AutoFilterMode = True Then
        ' do nothing
        Else
        ActiveSheet.Range("A1:S1").AutoFilter
        End If
        
        ' basic formatting
        Range("A:A,C:D,I:I").Delete
        Range("A1:O1").Interior.ColorIndex = 37
        Range("A1:O1").WrapText = True
        Columns("A:C").EntireColumn.AutoFit
        Columns("D:E").ColumnWidth = 15
        Columns("F").ColumnWidth = 20
        Columns("G:H").ColumnWidth = 8
        Columns("I").ColumnWidth = 10
        Columns("J").ColumnWidth = 20
        Columns("K").ColumnWidth = 9
        Columns("L:M").ColumnWidth = 20
        Columns("N:O").ColumnWidth = 10
        Rows("2:2").Select
        ActiveWindow.FreezePanes = True
        
        ' copying the allevents sheet for noMainsEnds data
        Sheets("AllEvents").Copy After:=Sheets("AllEvents")
        ActiveSheet.Name = "noMainsEnds"
        
        ' filter to exclude mains and ends
        Range("F:F").AutoFilter Field:=6, Criteria1:=Array( _
        "Foreign Dialogue-No Subtitles", "Main Title", "Narrative Title", "On-Screen Text" _
        , "Subtitle"), Operator:=xlFilterValues
        
       ' copying the allevents sheet for sub decisions data
        Sheets("AllEvents").Copy After:=Sheets("noMainsEnds")
        ActiveSheet.Name = "SubDecisions"
        
        ' filter sub decisions
        Range("D:D").AutoFilter Field:=4, Criteria1:="Subtitle"
        
        Sheets("AllEvents").Select
        
        
    Next
    Application.ScreenUpdating = True
    
' everything below is new test code


' insert row


'Range("A1").EntireRow.Insert


' adjust row height


'Rows(1).RowHeight = 17


' create combo box for tracker views


Worksheets("AllEvents").DropDowns.Add(0, 0, 100, 15).Name = "TrackerViews"


    ' add values to the tracker views combo box


    With Worksheets("AllEvents").Shapes("TrackerViews").ControlFormat
        .AddItem "All Events"
        .AddItem "No Mains & Ends"
        .AddItem "Sub Decisions"


    End With
    
        
End Sub




Private Sub Worksheet_Change(ByVal Target As Range)


    'Application.EnableEvents = False
    
    'If Not Intersect(Target, Range("A1")) Is Nothing Then
        'Select Case Target.Value
        
        If Target.Count > 1 Then Exit Sub
        If Target.Address <> "$A$1" Then Exit Sub
        Select Case Target
        
            Case "All Events"
                ActiveSheet.ShowAllData
                
            Case "No Mains & Ends"
                ActiveSheet.ShowAllData
                ActiveSheet.Range("$A$1:$O$1493").AutoFilter Field:=6, Criteria1:= _
               "Main Title"
               
            Case "Sub Decisions"
                
                ActiveSheet.ShowAllData
                ActiveSheet.Range("$A$1:$O$1493").AutoFilter Field:=4, Criteria1:= _
                "Subtitle"
                
            Case Else
                
        End Select
    End If
    Application.EnableEvents = True
    
End Sub


'Private Sub Worksheet_Change(ByVal Target As Range)


    'If Not Intersect(Target, Range("A1")) Is Nothing Then
        'Select Case Range("A1")
        
            'Case "All Events": ResetFilter
            'Case "No Mains & Ends": MainsEndsFilter
            'Case "Sub Decisions": SubDecisionsFiler
        'End Select
        


'End Sub


'Sub DropDown_Click(ByVal Target As Range)


'Dim drpdwn As DropDown
'Set drpdwn = ActiveSheet.DropDowns(Application.Caller)
'Select Case drpdwn.ListIndex


    'If Target.Address = "$A$1" Then
    'Select Case Target.Value
    
    'Case "All Events"
            'Call ResetFilter
            'module 8
            
    'Case "No Mains & Ends"
            'Call MainsEndsFilter
            'module 5
            
    'Case "Sub Decisions"
            'Call SubDecisionsFilter
            'module 7
    
    'Case Else 'do nothing
    
    'End Select
    


'If Not Intersect(Target, Range("A1")) Is Nothing Then
    
    
    'Select Case Range("A1")
        
        
            
    'End Select
    
'End Sub
 
1) You can get rid of that code if like. Some times if a macro fails & EnableEvents has been turned off, it won't get turned back on again. Which means that the Worksheet_Change event won't run. All the code from post 10 does is turn Events back on.
2) Adding code programmatically can be done, but it's not something I know much about.
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top