Finding a value

cecil
StarLounger
Posts: 98
Joined: 09 Sep 2010, 16:01

Finding a value

Post by cecil »

I want to retrieve the value in column B, which corresponds with text in column A. For example I might look for the keyword "DBServerName" in column A and retrieve "Oracle_Server_1" from column B. Currently I am using a for/next loop, with a compare in each itteration, and it works fine. Is there a better way, like a Vlookup for VBA?

Just wondering. Thanks.

User avatar
Jan Karel Pieterse
Microsoft MVP
Posts: 656
Joined: 24 Jan 2010, 17:51
Status: Microsoft MVP
Location: Weert, The Netherlands

Re: Finding a value

Post by Jan Karel Pieterse »

Application.WorksheetFunction.vlookup perhaps?
Regards,

Jan Karel Pieterse
Excel MVP jkp-ads.com

User avatar
HansV
Administrator
Posts: 78488
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Finding a value

Post by HansV »

To expand on Jan Karel's reply:

Code: Select all

Dim strKeyword As String
Dim strResult As String
Dim rngRange As Range
strKeyword = "DBServerName"
Set rngRange = ActiveSheet.Range("A:B")
strResult = Application.WorksheetFunction.VLookup(strKeyWord, rngRange, 2, False)
Best wishes,
Hans