trouble with locating a specific cell when using HLookup

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
476
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I need to capture what is in the cell in row 8, and 1 column over from what matches the cboEN value... the cboEN value is determined from a name that the user selects from a dropdown box on a userform.

In the picture that I have as an example, if cboEN = "Bell, Don", then I need to capture (as the assigned ckEMP value shown in the code below) the contents that are in cell AE8... (which is the string "Oni")

Bell.PNG

My code for this is:


VBA Code:
Private Sub cboEN_Exit(ByVal Cancel As MSForms.ReturnBoolean)
' Locate EMPLOYEE NAME from ComboBox selection

Dim ckEMP As String

ckEMP = Application.WorksheetFunction.HLookup((Me.cboEN), Object2.Range("DynamicRange"), 8, 0)

If ckEMP = "Oni" Then .chkOninEMP1 = True Else


So what I am trying to make happen is if "Oni" appears in the cell, then a checkbox on the userform that assigned to that value (to the "Oni" value) becomes 'ticked' after the userform is populated based on the cboEN value (which in this case is "Bell, Don" in row 1 on the worksheet "Object2")

Lastly, DynamicRange covers a large area on the object2 worksheet beginning in Row 1... which is the row that contains the names that cboEN is matching up to. (hope that makes sense! )

Thanks for any help or assistance from anyone willing to help!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about
VBA Code:
Dim ckEMP As Range

Set ckEMP = Object2.Range("DynamicRange").Find(Me.cboEN, , , xlWhole, , , False, , False)
If ckEMP Is Nothing Then Exit Sub
If ckEMP.Offset(7).Value = "Oni" Then .hkOninEMP1 = True
 
Upvote 0
How about
VBA Code:
Dim ckEMP As Range

Set ckEMP = Object2.Range("DynamicRange").Find(Me.cboEN, , , xlWhole, , , False, , False)
If ckEMP Is Nothing Then Exit Sub
If ckEMP.Offset(7).Value = "Oni" Then .hkOninEMP1 = True
Thank you, Fluff.

Unfortunately that didnt work.

First I thought it might be how that cell (the one containing "Oni") might be formatted. I checked, and it was a date format. I changed it to General, then to Text. Both didn't change the result.

Next I tried a couple different numbers for the offset number... you had (7) which was in your code- but each time it still didnt work or produce any different result.

Also, the code runs without any error, but it just doesnt give the correct return for what the ckEMP value should be... (which in the example I am using having the cboEN value equal "Bell, Don", it should be "Oni".)

So as a last resort, I tried having it show me what was actually getting returned as the value of "ckEMP". I added "MsgBox ckEMP" to the code. Each time the messagebox returns the value of cboEN (again, in this case, the name "Bell, Don".) It returns the same thing regardless what I use for the ".Offset" number (I tried both (7) and (8)... both results with the MsgBox showing me the name "Bell, Don".)

Any suggestions or ideas?

Thank you again, Fluff.
-Keith
 
Upvote 0
I added "MsgBox ckEMP" to the code. Each time the messagebox returns the value of cboEN (again, in this case, the name "Bell, Don".)
That's exactly what it should do.

What does this message box return
VBA Code:
Set ckEMP = Object2.Range("DynamicRange").Find(Me.cboEN, , , xlWhole, , , False, , False)
If ckEMP Is Nothing Then Exit Sub
MsgBox "|" & ckEMP.Offset(7).Value & "|"
If ckEMP.Offset(7).Value = "Oni" Then .hkOninEMP1 = True
 
Upvote 0
Oops, forgot to move one column, it should be
VBA Code:
If ckEMP.Offset(7, 1).Value = "Oni" Then .hkOninEMP1 = True
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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