简体   繁体   中英

vba how to match cell with range and return value in adjacent cells?

Excuse my lack of coding language, I'm just getting into vba coding. I have a list of Manufacturers (names) with contact details on one that I like to be populated on my "SPECSHEET" automatically when choosing manufacturer.

Worksheet "SPECSHEET" is fully created from macro.

Worksheet "CONTACTS" has a list of names in column A and phone numbers in column C

Worksheet "DATASHEET" holds information about each luminaire.

I am successful in showing the right manufacturer name:

Worksheets("SPECSHEET").Cells(Company, 5).Value = Worksheets("DATASHEET").Cells(1, 5).Value

But I like the row below to show the phone number:

If Worksheets("SPECSHEET").Cells(3, 4).Value = Worksheets("CONTACT").Range(A:A).Value Then
Worksheets("SPECSHEET").Cells(3, 5).Value = Worksheets("CONTACT").Cells(B "name adjacent to the matched name in column A) 
End If

Thanks for any help.

Try this:

Dim t
t = Application.Match(Worksheets("SPECSHEET").Cells(3, 4).Value, Worksheets("CONTACT").Range("A:A"), 0)
If Not IsError(t) Then
    Worksheets("SPECSHEET").Cells(3, 5).Value = Worksheets("CONTACT").Range("B" & t)
Else
    Worksheets("SPECSHEET").Cells(3, 5).Value = "Not Found"
End If

It uses the worksheet function MATCH() to find the row and then returns the value in Column B of that row. If it is not found then it returns Not Found .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM