Search code examples
excelvbauserform

Seeing error: Could not set the list property. Type mismatch


I am seeing the error Could not set the list property. Type mismatch when I run the following code.

It works correctly with For i = 1 To 10 but if I change it to 10 to 100 I see the error.

Private Sub CommandButton1_Click()
Dim myString
Sheets("Welcome").Select
Range("W3").Select
myString = Range("W3")
UserForm1.TextBox1.Text = myString
Sheets("Welcome").Select
Range("AA4").Select
myString = Range("AA4")
UserForm1.TextBox2.Text = myString
Dim rng As Range
Dim ws As Worksheet
Dim i As Long
Set ws = Worksheets("August")
Set rng = ws.Range("G2:AK2")
Set fnd = rng.Find(TextBox1)
    If fnd Is Nothing Then MsgBox TextBox1 & " not found": Exit Sub
Set first = fnd
    With ListBox1
    .Clear
        For i = 1 To 100
            .AddItem Worksheets("August").Range("B" & i + 5).Value
            .List(.ListCount - 1, 1) = fnd.Offset(i + 3, 0)
        Next i
    End With

End Sub

Solution

  • I've tested the code below with some random numbers for a data sample and it worked just fine. Some things to keep in mind while writing your code, as commented above, indenting helps tremendously. Secondly, I strongly recommend always using Option Explicit as the first line in any code module. This prevents you from using undeclared variables (ie. misspelled).

    Private Sub CommandButton1_Click()
        Dim rng As Range
        Dim ws As Worksheet
        Dim i As Long
        Dim fnd As Range                                    <=== Defined as a Range
            UserForm1.TextBox1 = Sheets("Welcome").Range("W3")
            UserForm1.TextBox2 = Sheets("Welcome").Range("AA4")
            Set ws = Worksheets("August")
            Set rng = ws.Range("G2:AK2")
            Set fnd = rng.Find(TextBox1)
            If fnd Is Nothing Then MsgBox TextBox1 & " not found": Exit Sub
            With ListBox1
                .Clear
                For i = 1 To 100
                    .AddItem Worksheets("August").Range("B" & i + 5).Value
                    .List(.ListCount - 1, 1) = fnd.Offset(i + 3, 0)
                Next i
            End With
    End Sub
    

    In the above code, note that I have shortened up the code used to set the value of the text boxes. Your original code:

    Dim myString
    
    
    Sheets("Welcome").Select
    Range("W3").Select
    myString = Range("W3")
    
    UserForm1.TextBox1.Text = myString
    
    Sheets("Welcome").Select
    Range("AA4").Select
    myString = Range("AA4")
    
    UserForm1.TextBox2.Text = myString
    

    became

    UserForm1.TextBox1 = Sheets("Welcome").Range("W3")
    UserForm1.TextBox2 = Sheets("Welcome").Range("AA4")
    

    This is much cleaner. This works without the .Text on the end of TextBox1 because the .Text is the default property. Also note that I directly referenced the cell with the string that I wanted without "Selecting" it. Selecting the cell is not necessary to work with it, selecting the cell is only required by a user operating a mouse.