简体   繁体   中英

VBA/Excel: Get cell from different sheet

Long story short: I have two sheets in Excel: Sheet1 which has two cells (A1 & A2) and Sheet2 (providing some data) which looks like this:

  | A | B  |
--|---|----|
1 | a | 5  |
2 | b | 10 |
3 | c | 15 |
4 | ...

So here's my problem: in Sheet1 when I enter "b" in A1 it should automatically enter 10 in A2.

Kinda like a simple find operation which will return the cell (or column) number and use that to return the value for the cell right next to it. But how do I do this?

Thanks in advance!

As mentioned before, a VLOOKUP is the simplest and fastest way but I assume you do not want to have a formula in that cell or on that whole Sheet1 for that matter? Here is one simple way to do the same in VBA while still using a VLOOKUP - just not on Sheet1:

Use Sheet2!Z1 for calculation, if that cell is used in your workbook, just use any other unused cell in an area that is hidded for example. Formula in Sheet2!Z1 :

=VLOOKUP(Sheet1!$A$1,Sheet2!$A$B,2,FALSE)

Simple VBA to populate Sheet1!A2:

sum DoIt
  Worksheets("Sheet1").Range("A2").Value = Worksheets("Sheet2").Range("Z1").Value
  end sub

You can call the DoIt sub with a button for example and it will keep your Sheet1 / cell A2 clean of formulas.

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