UserForm Listbox Clearing Before Repopulating

nygex

New Member
Joined
Jan 19, 2010
Messages
10
I'm stuck on what I'm doing wrong here and hoping there is any easy solution. The code works fine without the .clear but I'm just adding to the list each time I select a new option button and I want to clear the old data first.

VBA Code:
Private Sub LoadData1()
Dim ws As Worksheet
Set ws = Worksheets("Detail")
    
    ListBox1.Clear '<<===This causes a problem no matter where I place it.
    'I even created a separate button just to clear and still creates an issue when running
    'the below

    For irow = 18 To 850
    If Trim(ws.Range("AH" & irow).Value) = 1 Then
        With ListBox1
    .ColumnCount = 5
    .ColumnWidths = "40;80;80;80;5"
    .AddItem
    .List(i, 0) = Trim(ws.Range("O" & irow).Value) '<<===I get the error here
    .List(i, 1) = Trim(ws.Range("P" & irow).Value)
    .List(i, 2) = Trim(ws.Range("Q" & irow).Value)
    .List(i, 3) = Trim(ws.Range("S" & irow).Value)
    .List(i, 4) = "*"
    
    i = i + 1
            
        End With
    End If
Next irow

    Dim Arg1 As Range
    Dim Arg2 As Range
    
    Set Arg1 = ws.Range("AQ18:AQ847")
    Set Arg2 = ws.Range("AH18:AH847")
     
    txtSalaryAmt.Value = Format(Application.WorksheetFunction.SumIfs(Arg1, Arg2, 1) * 12, "#,###")

OptionButton3.Value = True

End Sub
 
OMG, I just fixed it. I removed "Dim i as Integer" at the top and the code runs fine and pretty fast. I only removed because that was the ONLY difference I could find.

Thanks so much for your help. I'm sorry I wasted your time but you did help me to focus on something outside the primary code since yours worked.
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
May throw an Option Explicit at the top of your modules. That may have avoided the problem to begin with.
 
Upvote 0
Because you declared "i" at the module level, it keeps its value. So when you ran your code & cleared the listbox "i" would not have been 0, so you additem on row 0 & try to put values on row "i".
Hope that makes sense.
 
Upvote 0

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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