Drop Down Box example

mgfacioli

New Member
Joined
Dec 29, 2009
Messages
19
I created this macro to include a Drop Down Box in a worksheet, although I think it is a bit old-fashioned. Any suggestions to make it better?

Code:
Option Explicit
Option Private Module
'---------------------------------------------------------------------------------------
' Module    : M_DropDownBox
' Author    : facioli
' Date      : 15/05/2015
' Purpose   : create a Drop Down Box with months
'---------------------------------------------------------------------------------------

Sub create_DropDownBox()
    Dim wkbLisBx As Workbook
    Dim wksListb As Worksheet
    Dim rngMeses As Range
    Dim aMeses As Variant
    
    Set wkbLisBx = ThisWorkbook
    Set wksListb = wkbLisBx.Worksheets("Listbox")
    aMeses = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
    
    InitializeDDBox wksListb, aMeses, Add_DDBox(wksListb, 100, 20, 80, 20), "DDB_01"
    InitializeDDBox wksListb, aMeses, Add_DDBox(wksListb, 200, 20, 80, 20), "DDB_02"
    
    'deleteDDBox wksListb
    Set wksListb = Nothing
    Set wkbLisBx = Nothing
End Sub


Function Add_DDBox(ByRef wksListb As Worksheet, ByVal iLeft As Integer, _
    ByVal iTop As Integer, ByVal iLarg As Integer, ByVal iAlt As Integer) As DropDown
'---------------------------------------------------------------------------------------
' Function : Add_DDBox
' Author    : facioli
' Date      : 15-05-2015
' Purpose   : create a Drop Down object
'---------------------------------------------------------------------------------------
'
    With wksListb
        .Select
        Set Add_DDBox = .DropDowns.Add(iLeft, iTop, iLarg, iAlt)
    End With
        
End Function


Sub InitializeDDBox(ByRef wksListb As Worksheet, ByRef aMeses As Variant, ByRef DDB1 As DropDown, ByVal strNomeDDB As String)
'---------------------------------------------------------------------------------------
' Procedure : InitializeDDBox
' Author    : facioli
' Date      : 15-05-2015
' Purpose   : initialize the object with months and assigns a macro to execute every time a item is selected
'---------------------------------------------------------------------------------------
'

    With DDB1
        .RemoveAllItems
        .List = aMeses
        .ListIndex = 1
        .OnAction = "getval_DDBox"
        .Name = strNomeDDB
        '.Locked = True
    End With
End Sub


Sub getval_DDBox()
'---------------------------------------------------------------------------------------
' Procedure : getval_DDBox
' Author    : facioli
' Date      : 15-05-2015
' Purpose   : the macro to be executed
'---------------------------------------------------------------------------------------
'
    Dim wkbLisBx As Workbook
    Dim wksListb As Worksheet
    Dim DDBox1 As DropDown
    Dim iMesAnterior As Integer, iMesAtual As Integer
    Dim iMes As Integer
    
    Set wkbLisBx = ThisWorkbook
    Set wksListb = wkbLisBx.Worksheets("Listbox")
    Set DDBox1 = wksListb.DropDowns(Application.Caller)
    
    iMes = DDBox1.ListIndex

End Sub


Private Sub deleteDDBox(ByRef wksListb As Worksheet)
'---------------------------------------------------------------------------------------
' Procedure : deleteDDBox
' Author    : facioli
' Date      : 25-01-2010
' Purpose   : to delete an especific Drop Down
'---------------------------------------------------------------------------------------
'
    Dim DDBox1 As DropDown
    
    Set DDBox1 = wksListb.DropDowns(2)
    
    DDBox1.Delete
End Sub


Private Sub delete_all_DDBox()
'---------------------------------------------------------------------------------------
' Procedure : deleteListobox
' Author    : facioli
' Date      : 25-01-2010
' Purpose   : to delete all Drop Downs in target worksheet
'---------------------------------------------------------------------------------------
'
    Dim wkbLisBx As Workbook
    Dim wksListb As Worksheet
    Dim ctControls As Controls
    Dim DDBox1 As DropDown
    
    Set wkbLisBx = ThisWorkbook
    Set wksListb = wkbLisBx.Worksheets("Listbox")
    
    For Each DDBox1 In wksListb.DropDowns
        Debug.Print DDBox1.Name
        DDBox1.Delete
    Next DDBox1

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,215,316
Messages
6,124,226
Members
449,148
Latest member
sweetkt327

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