ComboBox default value on Workbook_Open()

simurq

Board Regular
Joined
Nov 11, 2011
Messages
73
This is the code (object module) stored under Sheet1:

Code:
Public RegNo As Range

Public Sub ComboBox1_DropButt*******()
    Dim xCols As Integer
    Dim i As Integer
    Dim j As Integer
    Dim Temp As String
    Dim title As String
    Dim arrStr1() As Variant
    Dim arrStr2() As Variant
        
    Set RegNo = Range("$X$4")
    xCols = Sheet2.Cells(2, Columns.Count).End(xlToLeft).Column
    ReDim Preserve arrStr1(xCols - 2)
    ReDim Preserve arrStr2(xCols - 2)
    
    With Sheet2
        If ComboBox1.ListCount > xCols - 2 Then
             ComboBox1.Clear
        End If
        
    'Concatenate and remove double space in object names
        For i = 2 To xCols
            Temp = .Cells(2, i).Value & " " & .Cells(3, i).Value & " " & .Cells(4, i).Value
            title = Replace(Temp, "  ", " ")
            arrStr1(i - 2) = title
        Next i
    End With
    
    'Assign values of array 1 to array 2
    'to prepare for comparison of arrays
    For i = LBound(arrStr1) To UBound(arrStr1)
        arrStr2(i) = arrStr1(i)
    Next i
    
    'Sort array items alphanumerically
    For i = LBound(arrStr2) To UBound(arrStr2) - 1
        For j = i + 1 To UBound(arrStr2)
            If arrStr2(i) > arrStr2(j) Then
                Temp = arrStr2(j)
                arrStr2(j) = arrStr2(i)
                arrStr2(i) = Temp
            End If
        Next j
    Next i
    
    'Populate combo-box with sorted array items
    For i = LBound(arrStr2) To UBound(arrStr2)
        ComboBox1.AddItem arrStr2(i)
    Next i
    
    'Compare arrays and find corresponding column number on Sheet2
    For i = LBound(arrStr2) To UBound(arrStr2)
        If ComboBox1.Value = arrStr1(i) Then
            RegNo.Value = Sheet2.Cells(5, i + 2)
        ElseIf ComboBox1.Value = "" Then
            RegNo.Value = ""
        End If
    Next i
End Sub

And this is the other one stored under "ThisWorkbook" and calling the first procedure upon opening the workbook. What I try to achieve here is to set default value to combobox when the workbook opens:

Code:
Private Sub Workbook_Open()
    Sheet1.ComboBox1_DropButt*******
    Sheet1.ComboBox1.ListIndex = 0
End Sub

Both modules work OK to some extent but I have two problems:

1) Upon opening the workbook a value of ComboBox1 (ListIndex = 0) must be sought for in Sheet2 and the found value assigned to Range("X4"). This works OK when I drop down the combobox menu (since it's linked to "DropButt*******" event ) but NOT on Workbook_Open() although it calls the same ComboBox1 on run-time. How can I achieve the same functionality in both procedures?

2) When I use the first code ONLY, I can select combo box items from the menu. When I use both - I cannot! Is it somehow related to "ComboBox1.Clear" method?

Thank you very much!!!
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,791
Messages
6,121,611
Members
449,038
Latest member
apwr

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