简体   繁体   English

如果名称为X,则在Excel中从Z工作表中插入Y电话号码

[英]If name is X insert Y phone number from Z worksheet in Excel

I am trying to create a simple macro or VBA that will insert a phone number in column Y for value of someones name in Column X from a directory worksheet. 我正在尝试创建一个简单的宏或VBA,该宏或VBA将在Y列中插入电话号码,以获取目录工作表中X列中某人的姓名。

So for example, worksheet 1 (directory) has names of individuals and phone numbers: 因此,例如,工作表1(目录)具有个人名称和电话号码:

John Smith 123 456 789
Joe Garden 555 555 555
Jill Spill 999 999 999

When creating a new worksheet 2, when I put John Smith in Column A, I want it to automatically populate 123 456 789 in Column B. 创建新工作表2时,当我将John Smith放在A列中时,我希望它自动在B列中填充123 456 789。

Thanks for the help! 谢谢您的帮助!

Try this code and let me know if there's anything that I missed. 尝试这段代码,让我知道是否有任何我想念的东西。 Be sure to change Sheet4 to the name of the sheet that has the phone # list. 确保将Sheet4更改为具有电话号码列表的工作表的名称。

Sub Macro12()
   Range("B1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],Sheet4!C[-1]:C,2,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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