having trouble understanding how(why) its finding this one cell when using VLookup in a userform(?)

kbishop94

Active Member
Joined
Dec 5, 2016
Messages
458
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a userform with a combobox to select names on a worksheet. The names and associated data is located in a named range (DynamicRange)

Below are some screen shots and the code that I curerntly have:

my userform showing where I am choosing the name "Aimes, Kelly"
15p0bck.jpg
:



and after I leave that combobox (cboEN is the name of the combobox)

I want it to be able to find that name here:
358rw4x.jpg



So here is my code:

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

If WorksheetFunction.CountIf(Object2.Range("Names"), Me.cboEN.Value) = 0 Then
MsgBox "Employee Not Found."
Me.cboEN.Value = ""
Cancel = True
Exit Sub
End If

With Me

cK1 = Application.WorksheetFunction.VLookup((Me.cboEN), Object2.Range("DynamicRange"), 1, 2)
If cK1 = "b" Then .chkNew1 = True

MsgBox cK1
MsgBox cboEN

End With
End Sub


Now, I know its working, because I can confirm what its finding:
it finds 'a' which is row 4 in the column that the name "Aimes, Kelly" is in, and its also finding the correct name ("Aimes, Kelly") (combined 2 screen shots so you can see both message boxes):

29gj61z.jpg





So what I cant figure out is why it seems no matter what I change these numbers to (in red):
Code:
cK1 = Application.WorksheetFunction.VLookup((Me.cboEN), Object2.Range("DynamicRange"), [B][COLOR=#ff0000]1, 2[/COLOR][/B])
If cK1 = "b" Then .chkNew1 = True

I only seem to get "a" or "b"... ??

All the other times I use this method of VLookup, I always have the data I am trying to retrieve its in ROWS, and this time the data associated with my lookup is in columns (2 columns at that... the one directly below the name, and the column immediately to the right of the name column.)

For instance, lets say I want to retrieve the "R" that is 4 rows directly below the name "Aimes, Kelly" ("R" is located in column N and row 5 and I have it oulined in GREEN in the screenshot below)
What would I have to use to find this location?

2mmznuv.jpg


Please and Thank you for any help you guys can offer me.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
This is probably useful information as well:

Here is the range of my dynamic range (aptly named) "DynamicRange".
It starts at column H and goes down to the bottom (row 63) and over to the last column (which is currently PE)
dbgikx.jpg
 
Upvote 0
For instance, lets say I want to retrieve the "R" that is 4 rows directly below the name "Aimes, Kelly" ("R" is located in column N and row 5 and I have it oulined in GREEN in the screenshot below)
What would I have to use to find this location?

Maybe this way :

Code:
a = Application.Match("Aimes, Kelly", Object2.Range("DynamicRange").Rows(1), False)
txt = Object2.Range("DynamicRange").Cells(5, a)

You'll find that txt = "R"
 
Upvote 0
Maybe this way :

Code:
a = Application.Match("Aimes, Kelly", Object2.Range("DynamicRange").Rows(1), False)
txt = Object2.Range("DynamicRange").Cells(5, a)

You'll find that txt = "R"
Thanks, Akuini.

However, I get this error:

2mhwpiq.jpg


2po9qa9.jpg


Since row 4 is all either 'a' 's or 'b' 's, what about finding the specific 'a' that its findind and illuminating that specific cell on the sheet? (because the entire row 4 is either an 'a' or 'b')

What can i do to have it do to find SOMETHING on that sheet that can serve as a reference to pinpoint where it is on the sheet and then I can work from that to locate a specific cell or range?

I thought this would locate and show me 'cK1', but I cant seem to see what its highlighting... (?)

Code:
Range("cK1").Interior.Color = 65280

Any suggestions?

Thank you
 
Upvote 0
Whilst I haven't a clue what you are trying to do, you are using vertical lookup, when the names are in a row, try
Code:
cK1 = Application.WorksheetFunction.HLookup((Me.cboEN), Object2.Range("DynamicRange"), 5, 0)
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

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