Search code examples
excelcomboboxuserformmismatchvba

Why I get error when trying to open the dropdown list in a combobox


I get a mismatch error when trying to open the dropdown list in a combobox in a userform

I've read this: https://msdn.microsoft.com/en-us/library/aa264979(v=vs.60).aspx (Didn't understand it.)

The sub that gets the error

Private Sub FailureComBox_DropButtonClick()

Dim emptyRow As Long, i As Integer

emptyRow = WorksheetFunction.CountA(Sheets("Fel").Range("A:A")) + 1

For i = 2 To emptyRow
    FailureComBox.AddItem Cells(i, "A")
Next i

End Sub

I get an mismatch error on the line that reads: FailureComBox.AddItem Cells(i, "A")

My entire code for the userform

Private Sub BackCB_Click()

Unload Me
UserForm1.Show

End Sub

Private Sub ExitCB_Click()

Unload Me

End Sub

Private Sub FailureComBox_Change()

FailureComBox.Text = FailureComBox.Value

End Sub

Private Sub FailureComBox_DropButtonClick()

Dim emptyRow As Long, i As Integer

emptyRow = WorksheetFunction.CountA(Sheets("Fel").Range("A:A")) + 1

For i = 2 To emptyRow
    FailureComBox.AddItem Cells(i, "A")
Next i

End Sub

Private Sub OkCB_Click()


Dim emptyRow As Long

emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

If IsDate(DateTB) = False Then
    MsgBox "Var vänlig ange ett korrekt datum"

ElseIf FailureComBox <> "" And StopComBox <> "" Then
    MsgBox "Var vänlig välj bara ett stopp eller fel."
    
Else
    
    Cells(emptyRow, 1).Value = DateTB.Value
    
    If SLD000OB = True Then
        Cells(emptyRow, 2).Value = SLD00OB.Caption
    
    If SLD00OB = True Then
        Cells(emptyRow, 2).Value = SLD00OB.Caption
        
    If SLD1OB = True Then
        Cells(emptyRow, 2).Value = SLD1OB.Caption
        
    If SLD2OB = True Then
        Cells(emptyRow, 2).Value = SLD2OB.Caption

End Sub

Solution

  • Dim a As String
    
    For i = 3 To emptyRow
        FailureComBox.AddItem a
        a = Sheets("StopFel").Cells(i, "D")
    Next i
    

    This fixed it for me. apparently .AddItem got a problem with setting a cell directly as its value.