Could someone please check my IF statement?

Adam88

New Member
Joined
Dec 12, 2017
Messages
16
Hi Guys,

Hope you all had a great New Years!

First day back in work and I'm already at a stumbling block... Can anyone please offer advice on why this IF statement is trying to carry out the code after Else even though the initial part of the IF statement is TRUE?
Code:
Private Sub cbbEmployee_Change()

Dim EmpName As Variant

If IsNull(Me.cbbEmployee.Value) Then
    txtHourly.Value = "0"
Else
    EmpName = cbbEmployee.Value
    txtHourly.Value = WorksheetFunction.VLookup(EmpName, Worksheets("Formulas").Range("T1:U53"), 2, False)
End If

End Sub

It's a combo box, I select an employee and the Vlookup pulls in their hourly rate from another worksheet. Once the user clicks Save, it runs;
Code:
me.cbbEmployee.Clear

Which then prompts this section of code. Its supposed to check for Null values and then just populate the txtHourly box with a 0 if nothing is detected as selected in the combobox. I receive an error 1004 "Unable to get the Vlookup property etc etc" which means its trying to carry out the latter part of the code even though the combobox has nothing in it!

Hope you can help
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Display the length of the combobox value before the If.

result should be 0, if it's not then you have a value in the combobox which is therefore not a null hence the "Else" part of the code being executed.
 
Upvote 0
Thank you for your advice.

I've F8'd my way through the code and added a line to check the combobox value prior to the IF statement, it's coming back as ""

Is there a way I can set this so that it satisfies the IsNull criteria?

Thanks
 
Upvote 0
That's weird.
If I place a value in A1 and run this code

Code:
If Isnull(Range("A1")) then
    MsgBox "is null"
else
    Msgbox "is not null")

I get "is not null" which is correct.
If I then blank out the value in A1...

I still get "is not null"

Can't you change your code to

Code:
If (Me.cbbEmployee.Value="" Then
 
Upvote 0
After reading that link you sent and also your original reply to check length, I've adapted the code to now do this;

Code:
If Len(Me.cbbEmployee.Value) = 0 Then
    txtHourly.Value = "0"
Else
    EmpName = cbbEmployee.Value
    txtHourly.Value = WorksheetFunction.VLookup(EmpName, Worksheets("Formulas").Range("T1:U53"), 2, False)
End If

Which works perfectly! Thank you for the idea!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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