简体   繁体   中英

how do I get value from a sheet then show on another sheet in excel base on matching id

I have an ID and Name column in sheet A and in sheet BI have an ID column and an empty column
what I want to do is to fill the empty column with values from the Name column in sheet A where both the ID columns are matched
here is what I want to do in MySQL statement

select SheetA.Name from SheetA join SheetB on SheetA.id = SheetB.id

can someone help translate this to excel code?

Refer to screenshots when interpreting the ranges (which will require modification to suit your specific needs / Excel data list positioning)...

VB code:

Sub Macro1()
'
' Macro1 Macro
'

'
    ActiveCell.Formula2R1C1 = _
        "=INDEX('67650244 (2)'!R4C3:R7C3,MATCH('67650244'!R4C2:R7C2,'67650244 (2)'!R4C2:R7C2,0))"
    Range("C5").Select
    ActiveWindow.SmallScroll Down:=-3
End Sub

Screenshots

Sheet 1 (entitled "67650244"):

67650244

Sheet 2 (entitled "67650244 (2)"):

67650244 (2)

This assumes the source values you're looking up from are contained with the 2nd sheet (ie "67650244 (2)"), and the index/match function "places" the relevant ID/lookups into Sheet 1 (ie "67650244"). Further, the "activecell" is simply cell "C4" in sheet 1 (the function in the VB code above will insert the response/'looked-up' values as an array to fill all values adjacent to those in column B of this sheet (assuming a successful lookup, if not, then it will display "#Value." next to the relevant Lookup value that couldn't be found in sheet 2....

Note: VB using "Row/Column" referencing when using a function like this, where "R1C1. simply refers to cell A1 in excel, To decipher this more easily for writing up the applicable version of code for your case, you can go to "Tools, Options, Formula" in the left had pane (within Excel, not VB), and then select "R1C1 Reference Style" box: per screenshot below:

R1C1 参考样式选项

You can always change this back later (deselect the option) as desired the same way.

Note: it would be far easier / more convenient to just use the index(range to return, match(value to lookup, range to lookup, 0)) method within excel as opposed to within VB - but this depends on desired functionality of your setup/circumstances/excel model/etc.

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