Type Mismatch Error when Long = 0 in For loop

dmqueen

Board Regular
Joined
Aug 5, 2014
Messages
53
Good Morning,I am receiving a type mismatch error I cannot resolve. Any help would be greatly appreciated. :)It occurs when it enters a For loop for the last control, when the loop control = 0. Code below:I even tried recasting as a Variant, but it didn't help. I have marked the line with an asterisked comment.<code>Private Sub EnterButton_Click()Dim bCancel As Boolean: bCancel = FalseDim ws As Worksheet: Set ws = Sheets("Sheet1")Dim lastrow As Long,i As LongDim x As Long'''''''''''''''''''''''''''' Check for no selections'''''''''''''''''''''''''''If LBType.ListIndex = -1 Then MsgBox ("You must select a type!") bCancel = True LblType.ForeColor = RGB(255, 0, 0) ElseIf LBNumber.ListIndex = -1 Then MsgBox ("You must select a number!") LblNumber.ForeColor = RGB(255, 0, 0) bCancel = True ElseIf LBRotation.ListIndex = -1 Then MsgBox ("You must select a rotation!") LblRotation.ForeColor = RGB(255, 0, 0) bCancel = True ElseIf cmbNew.ListIndex = False ThenMsgBox ("Please select a new status!")lblNew.ForeColor = RGB(255, 0, 0)bCancel = TrueEnd IfIf bCancel = True Then Exit SubEnd Iflastrow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1For i = 0 To LBType.ListCount - 1 If LBType.Selected(i) Then ws.Range("A" & lastrow).Value = LBType.List(i) End IfNext iFor i = 0 To LBNumber.ListCount - 1 If LBNumber.Selected(i) Then ws.Range("B" & lastrow).Value = LBNumber.List(i) End IfNext iFor i = 0 To LBRotation.ListCount - 1 If LBRotation.Selected(i) Then ws.Range("C" & lastrow).Value = LBRotation.List(i) End IfNext i'The error occurs on the next line!*************************************************************************For x = 0 To cmbNew.ListCount - 1 If cmbNew.ListIndex(x) Then ws.Range("D" & lastrow).Value = cmbNew.ListIndex(x) End If Next xEnd Sub</code>
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi

Try the below code

Code:
Private Sub EnterButton_Click()
Dim bCancel As Boolean
bCancel = False
Dim ws As Worksheet
Set ws = Sheets("Sheet1")
Dim lastrow As Long, i As Long
Dim x As Long
'''''''''''''''''''''''''''' Check for no selections'''''''''''''''''''''''''''
If LBType.ListIndex = -1 Then
    MsgBox ("You must select a type!")
    bCancel = True
    LblType.ForeColor = RGB(255, 0, 0)
ElseIf LBNumber.ListIndex = -1 Then
    MsgBox ("You must select a number!")
    LblNumber.ForeColor = RGB(255, 0, 0)
    bCancel = True
ElseIf LBRotation.ListIndex = -1 Then
    MsgBox ("You must select a rotation!")
    LblRotation.ForeColor = RGB(255, 0, 0)
    bCancel = True
ElseIf cmbNew.ListIndex = False Then
    MsgBox ("Please select a new status!")
    lblNew.ForeColor = RGB(255, 0, 0)
    bCancel = True
End If


If bCancel = True Then
    Exit Sub
End If


lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 0 To LBType.ListCount - 1
    If LBType.Selected(i) Then
        ws.Range("A" & lastrow).Value = LBType.List(i)
    End If
Next i
For i = 0 To LBNumber.ListCount - 1
    If LBNumber.Selected(i) Then
        ws.Range("B" & lastrow).Value = LBNumber.List(i)
    End If
Next i
For i = 0 To LBRotation.ListCount - 1
    If LBRotation.Selected(i) Then
        ws.Range("C" & lastrow).Value = LBRotation.List(i)
    End If
Next i



ws.Range("D" & lastrow).Value = cmbNew.[COLOR=#ff0000]value[/COLOR]



End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,975
Members
449,200
Latest member
Jamil ahmed

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