Error when setting ListIndex for a ComboBox

mwollert

New Member
Joined
Jul 13, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have a UserForm (FrmImpCodeList) with a single ComboBox (CBImpCode) on it.

I am trying to set ListIndex to 1 for the ComboBox in the Userform_Initialization routine and I get an error when FrmImpCodeList.show is run from another routine. If I take out CBImpCode.ListIndex = 1 the the form will load and function correctly. I have other ComboBoxes on other forms that I use ListIndex property and have no problem when the form loads. It's not critical but driving me nuts why it is causing a problem.


VBA Code:
Private Sub UserForm_Initialize()
    Dim ImpStartRow, ImpEndRow, MyItem As Integer
'   populate array
  
    With Worksheets("Program Data")
        ImpStartRow = .Cells(33, 2)
        ImpEndRow = .Cells(34, 2)
            
        CBImpCode.AddItem "       -         NONE"
        
        For MyItem = 1 To ImpEndRow - ImpStartRow + 1
            CBImpCode.AddItem .Cells(MyItem + ImpStartRow - 1, 2) & "  -  " & .Cells(MyItem + ImpStartRow - 1, 1)
        Next MyItem
        
        CBImpCode.ListRows = MyItem
        CBImpCode.ListIndex = 1
    End With

End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
Originally you said the error was on this FrmImpCodeList.shownow you are saying it's on FrmImpoundCodeList.Show
Have you changed the name of the Userform?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

mwollert

New Member
Joined
Jul 13, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I cut the sheets out and can duplicate the error with a small workbook.


Double click any cell on "Savings" worksheet. Funny thing I'm noticing on this workbook, is that I get the same error, but the click routine of the ComboBox is putting the right number in the cell that is double clicked.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,286
Office Version
  1. 365
Platform
  1. Windows
The problem is that by setting the listindex you are triggering the combobox click event which in turn unloads the userform.

To fix the problem you could introduce a boolean variable that indicates the userform is being initialized and use it to prevent the userform being unloaded.
VBA Code:
Dim boolInitialize As Boolean

Private Sub CBImpCode_Click()
Dim rng As Range

    'identify cell range where you set the value
    Set rng = ThisWorkbook.ActiveSheet.Range(ActiveCell.Address)

    'set cell range value with Range.Value property

    rng = Left(CBImpCode.List(CBImpCode.ListIndex), 6)

End_Of_Sub:
    If Not boolInitialize Then
        Unload FrmImpoundCodeList
    End If

    boolInitialize = False
End Sub

Private Sub UserForm_Initialize()

'Start Userform Centered inside Excel Screen (for dual monitors)
    boolInitialize = True
    Me.StartUpPosition = 0
    Me.Left = Application.Left + (0.6 * Application.Width) - (0.4 * Me.Width)

    Me.Top = Application.Top + (0.6 * Application.Height) - (0.5 * Me.Height)

    Dim ImpStartRow, ImpEndRow, MyItem As Integer

    '   populate array

    With Worksheets("Program Data")

        ImpStartRow = .Cells(33, 2)
        ImpEndRow = .Cells(34, 2)

        CBImpCode.AddItem "       -         NONE"

        For MyItem = 1 To ImpEndRow - ImpStartRow + 1
            CBImpCode.AddItem .Cells(MyItem + ImpStartRow - 1, 2) & "  -  " & .Cells(MyItem + ImpStartRow - 1, 1)
        Next MyItem

        CBImpCode.ListRows = MyItem
        CBImpCode.ListIndex = 1

    End With

End Sub
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,135
Messages
5,640,321
Members
417,137
Latest member
Dashivas123

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
Top