User Form won't Initialize. Run time error

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello everyone:

I am having an issue with my form initializing. The form name is frmAddRepresentative and has a combo box called cboRepName. The code below should run when the form initializes. But I am getting a run time error -2147467259 (80004005). Unespecified error. Not much help from that.

Also, I change UserForm_Initialize to frmAddRepresentative_Initialize, the form opens but I get a subscript out of range on the Me.cboRepName references.

I can't figure out what I am doing wrong.

Code:
Option Explicit

Private Sub UserForm_Initialize()
Dim ws As Worksheet
Set ws = Worksheets("Rep Information")
 
UpdateComboLists
 
Me.cboRepName.SetFocus
 
End Sub
 
Private Sub UpdateComboLists()

Dim cName As Range
Dim ws As Worksheet
Set ws = Worksheets("Rep Information")
 
Me.cboRepName.Clear
For Each cName In ws.Range("repName")
  
  With Me.cboRepName
  
    .AddItem cName.Value
    .List(.ListCount - 1, 1) = cName.Offset(0, 1).Value
  
  End With
Next cName
 
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Also, I change UserForm_Initialize to frmAddRepresentative_Initialize

Please do not do this. Just use UserForm_Initialize and in the code module for the Userform.

Execute the macro that loads the userform by pressing F8 on that macro (repeatedly, line by line).
That way, you can debug the Initialize event.
 
Upvote 0
Thank you, I did as you said and the error seems to be in the For Loop,

For Each cName In ws.Range("repName")

But I can't figure out why there would be an error. I named the variable rngName and I am using a named range repName. This should be working.
 
Upvote 0
Are there any values in repName?

Have you previously used RowSource to populate the listbox?
 
Upvote 0
Thanks, just for kicks I changed the name of my named range from repName to Names and it works fine now. There must be something with the naming convention.
 
Upvote 0

Forum statistics

Threads
1,213,550
Messages
6,114,265
Members
448,558
Latest member
aivin

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