If Statement only evaluates "False"

ERyan

New Member
Joined
Aug 6, 2016
Messages
2
Hello,

I'm a new member here and this is my first post. I really appreciate any help you all might be able to offer. I am a bit new to VBA, but eager to learn, so your critique is welcome.

I am creating a userform which uses combo boxes to collect Account Name and Account Number, and Option buttons to collect Status (Active, Inactive) then adds the information to sheet (Accounts). I'm working on making this dynamic in that if someone attempts to enter an account name or account number that is already on the lsit the userform will search and recognize it is already on the list and auto-populate the rest of the form. I have it working perfectly when I enter an already existing Account Name in that combo box. The rest of the form auto-populates just as is should using the following code:

Rich (BB code):
Private Sub cboName_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim last As Integer, varcboName As String
    varcboName = ""
    With ActiveSheet
        last = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With
    
    For Each cell In Range("B3:B" & last)
        If cell.Value = Me.cboName.Value Then
            varcboName = cell.Value
            cboNumber.Value = cell.Offset(0, -1).Value
            If cell.Offset(0, 1).Value = "Active" Then
                optActive = True
            Else
                optInactive = True
            End If
            cmdUpdate.Enabled = True
        End If
    Next
    If varcboName <> Me.cboName.Value Then
        cmdAddNew.Enabled = True
    End If
End Sub

So I tried the same approach for the combo box which accepts the Account Number using the following code:

Rich (BB code):
Private Sub cboNumber_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim last As Integer, varcboNumber As Long
    varcboNumber = 0
    With ActiveSheet
        last = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    For Each cell In Range("A3:A" & last)
        If cell.Value = Me.cboNumber.Value Then
            varcboNumber = cell.Value
            cboName.Value = cell.Offset(0, 1).Value
            If cell.Offset(0, 2).Value = "Active" Then
                optActive = True
            Else
                optInactive = True
            End If
            cmdUpdate.Enabled = True
        End If
    Next
    If varcboNumber <> Me.cboNumber.Value Then
        cmdAddNew.Enabled = True
    End If
End Sub

This one does not auto-populate anything but it does not give an error message. I believe I've narrowed this down to the line in red above. This appears to always evaluate to False, even if I enter a number in the combo box which matches perfectly with a number in column A on my sheet. I think it has to do with some kind of mismatch between strings and integers but I have tried every variation with the variable call I can think of. Any help here would be greatly appreciated.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi and welcome to the MrExcel Message Board.

Try this line instead:
Code:
If CLng(cell.Value) = CLng(Me.cboNumber.Value) Then
That will convert both values to Longs before making the comparison.

Regards,
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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