ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Using Vlookup in VBA code (https://www.excelbanter.com/excel-programming/319757-using-vlookup-vba-code.html)

Kathy - Lovullo

Using Vlookup in VBA code
 
I am having some problems using the VLookup command within my VBA code. I
have successfully written some code which will pull the Novell User ID and
set it to strNovellUser.

I want to now use the strNovellUser in a VLookup function to lookup some
other information I have on another sheet (Users)in the workbook. I have
verified that my Novell UserID shows up in column 1 and there is data in
column 2, but the VLookup is either not finding it or it is not being set
properly to strName.

Below is part of the code I am using. Any help would be greatly appreciated.

===============
Public Sub BasicInfo()
Dim strName As String

strName = WorksheetFunction.VLookup(strNovellUser, _
Worksheets("Users").Range("A2:E14"), 2)
MsgBox "First Name is " & strName

End Sub
===============

Sharad Naik

Using Vlookup in VBA code
 
Hello Kathy,

Try with small modification as under (Added "FALSE")

strName = WorksheetFunction.VLookup(strNovellUser, _
Worksheets("Users").Range("A2:E14"), 2, FALSE)

If that too doesn't work try following (Further added "Trim")

strName = WorksheetFunction.VLookup(Trim(strNovellUser), _
Worksheets("Users").Range("A2:E14"), 2, FALSE)

Sharad

"Kathy - Lovullo" wrote in message
...
I am having some problems using the VLookup command within my VBA code. I
have successfully written some code which will pull the Novell User ID and
set it to strNovellUser.

I want to now use the strNovellUser in a VLookup function to lookup some
other information I have on another sheet (Users)in the workbook. I have
verified that my Novell UserID shows up in column 1 and there is data in
column 2, but the VLookup is either not finding it or it is not being set
properly to strName.

Below is part of the code I am using. Any help would be greatly
appreciated.

===============
Public Sub BasicInfo()
Dim strName As String

strName = WorksheetFunction.VLookup(strNovellUser, _
Worksheets("Users").Range("A2:E14"), 2)
MsgBox "First Name is " & strName

End Sub
===============




Dave Peterson[_5_]

Using Vlookup in VBA code
 
I would think you'd want your =vlookup() to have a 4th parm of false (exact
match required):

Public Sub BasicInfo()
Dim strName As Variant
strName = Application.VLookup(strNovellUser, _
Worksheets("Users").Range("A2:E14"), 2, 0)
if iserr(strname) then
msgbox "Not found"
else
MsgBox "First Name is " & strName
end if
End Sub

I like application.vlookup() so that I can check for an error. But that means I
have to declare strName as a variant (varName???).



Kathy - Lovullo wrote:

I am having some problems using the VLookup command within my VBA code. I
have successfully written some code which will pull the Novell User ID and
set it to strNovellUser.

I want to now use the strNovellUser in a VLookup function to lookup some
other information I have on another sheet (Users)in the workbook. I have
verified that my Novell UserID shows up in column 1 and there is data in
column 2, but the VLookup is either not finding it or it is not being set
properly to strName.

Below is part of the code I am using. Any help would be greatly appreciated.

===============
Public Sub BasicInfo()
Dim strName As String

strName = WorksheetFunction.VLookup(strNovellUser, _
Worksheets("Users").Range("A2:E14"), 2)
MsgBox "First Name is " & strName

End Sub
===============


--

Dave Peterson


All times are GMT +1. The time now is 05:19 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com