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
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
What are the values in B33 & B34 when you run the code?
 

mwollert

New Member
Joined
Jul 13, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
The values of B33 & B34 are 52 and 75.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,856
Office Version
  1. 365
Platform
  1. Windows
And if you remove the list index line, do you have 24 items in the combo + the None line?
 

mwollert

New Member
Joined
Jul 13, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Yes
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
What is the error message?
 

mwollert

New Member
Joined
Jul 13, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

mwollert

New Member
Joined
Jul 13, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
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
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,730
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,130,129
Messages
5,640,297
Members
417,135
Latest member
zeusmining

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