Error when setting ListIndex for a ComboBox

mwollert

New Member
Joined
Jul 13, 2020
Messages
27
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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
What are the values in B33 & B34 when you run the code?
 
Upvote 0
And if you remove the list index line, do you have 24 items in the combo + the None line?
 
Upvote 0
What is the error message?
 
Upvote 0
Thank you for your prompt reply. Unfortunately I’m not at my computer right now. I don’t have the exact error code right now. I’ll respond back in a while when I get back to the computer.

The debug shows FrmImpCodeLust.show is the line causes the error.
From the debug menu, compile does not show any errors.
 
Upvote 0
In the VB Editor, select Tools - Options, then on the General tab change the error handling to 'Break in class module' so that the actual error line will be highlighted.
 
Upvote 0
In the VB Editor, select Tools - Options, then on the General tab change the error handling to 'Break in class module' so that the actual error line will be highlighted.
Even with "Break in calls module" selected I am still getting the following error at FrmImpoundCodeList.Show :

Run-time error '91':
Object variable or With block variable not set
 
Upvote 0
You should only get an error there with that option set if the name of the form is wrong - but since you say the code works without your Listindex line, I am baffled. Any chance you can upload your workbook to a site like onedrive or dropbox and post a link here?
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,020
Members
448,543
Latest member
MartinLarkin

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