UserForm ListBox Crashes Program

03856me

Active Member
Joined
Apr 4, 2008
Messages
297
I am using Excel 2013, and have setup multiple userforms that work fine, until I add a ListBox. I am familiar with userforms and have setup 100's in 2003-2007 with no problems that include listboxes that refresh after data is added. Using 2013, if I use the same method and code, it crashes and reboots Excel every time.

Am I missing something in the code that must be added or modified in 2010-2013?
FYI - opening prior workbooks in Excel 2013 execute properly without modification, it is just setting up new listboxes that create the problem.

The code is attached to my Add command button. Any help is greatly appreciated.
Code:
'===== Add Button ======================
Private Sub cmd_add_Click()
    AddData
    ClearData
    
    Dim strRowSource As String
    With lstState
        strRowSource = .RowSource
        .RowSource = vbNullString
        .RowSource = strRowSource
    End With
End Sub

'===== Add Data =====================
Private Sub AddData()
Dim iRow As Long
Dim WS As Worksheet
Set WS = Worksheets("lookups")

iRow = WS.Cells(Rows.Count, 19) _
    .End(xlUp).Offset(1, 0).Row

    WS.Cells(iRow, 19).Value = Me.txt_state.Value
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Is this userform code? Where is the cleardata sub? What is 1ststate and txt_state. What is this code supposed to do? Maybe a bit more info. Dave
 
Upvote 0
This code is attached to my UserForm. I am attaching all the code for this form. There is only 1 textbox on the form, and I simply want to display a listbox displaying the list of states.

This codes works perfectly, but when I add a listbox it crashes Excel

Code:
Private Sub UserForm_Initialize()
    SortStateTable
End Sub

'===== Add Button ======================
Private Sub cmd_add_Click()
    AddData
    ClearData
    
    Dim strRowSource As String
    With lstState
        strRowSource = .RowSource
        .RowSource = vbNullString
        .RowSource = strRowSource
    End With
End Sub

'===== Clear Button =====================
Private Sub cmd_clear_Click()
    ClearData
End Sub

'===== Close Button =====================
Private Sub cmd_close_Click()
    Unload Me
    Worksheets("MaintMenu").Select
End Sub

Private Sub ClearData()
    Me.txt_state.Value = ""
    Me.txt_state.SetFocus
End Sub

'===== Force use of Close Button ========
Private Sub UserForm_QueryClose(Cancel As Integer, _
  CloseMode As Integer)
  If CloseMode = vbFormControlMenu Then
    Cancel = True
    MsgBox "Please use the button!"
  End If
End Sub

'===== Add Data =====================
Private Sub AddData()
Dim iRow As Long
Dim WS As Worksheet
Set WS = Worksheets("lookups")

iRow = WS.Cells(Rows.Count, 19) _
    .End(xlUp).Offset(1, 0).Row

    WS.Cells(iRow, 19).Value = Me.txt_state.Value
Application.ScreenUpdating = True
End Sub

'===== Sort State Table =====================

Sub SortStateTable()
    ActiveWorkbook.Worksheets("lookups").ListObjects("stateTable").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("lookups").ListObjects("stateTable").Sort. _
        SortFields.Add Key:=Range("stateTable[[#All],[State]]"), SortOn:= _
        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("lookups").ListObjects("stateTable").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
So U just add a listbox and it crashes. Is there no listbox code? What is 1ststate... oh that is LstState. It's best to be specific ie. UserformName.ListboxName.etc The rowsource setting for the listbox (if that is what the code is supposed to do?) seems all wrong. Please indicate what the listbox rowsource is supposed to be. Dave
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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