使用VLookup从工作表插入特定数据

有人可以帮助我 – 我有一个工作表,并在一个代码的条目我需要它通过从该行中的某些设置单元格,并显示在活动工作表。 这是我已经得到的,可能会给我一些VBA的无知的一些想法,这是行不通的。

Sub AddProduct() On Error GoTo MyErrorHandler: Dim Code As Long Code = B4 Sheets("Code Input Sheet").Range(A9) = Application.VLookup(Code,Worksheets("Cost Sheet").Range("A2:XFD1048576"), 1, False) ActiveCell.End(xlRight).Offset(0, 1).Select Selection = Application.VLookup(Code, Worksheets("Cost Sheet").Range("A2:XFD1048576"), 2, False) ActiveCell.End(xlRight).Offset(0, 1).Select Selection = Application.VLookup(Code, Worksheets("Cost Sheet").Range("A2:XFD1048576"), 5, False) ActiveCell.End(xlDown).Offset(1, 0).Select MyErrorHandler: If Err.Number = 1004 Then MsgBox "Code doesn't exist." End If End Sub 

希望这是有道理的谢谢


编辑

截图

我可能需要从头开始,但这里基本上是我需要的:用户在B4中input代码,通过在第二张纸上查找的button(如果存在)运行macros,并从该代码行中取出三个单元格到第一张纸上的A9:C9。 然后希望这个过程可以重复,数据将进入下一行。 希望这不是太多的问题!

而不是VLOOKUP使用Range.Find方法 :

 Sub AddProduct() Dim code As Variant Dim c As Range Dim ws As Worksheet Dim lastrow As Long Set ws = ActiveSheet code = ws.Range("B4") 'Find code and set c to the cell Set c = Worksheets("Cost Sheet").Range("A:A").Find(code) If c is Nothing Then 'if the code is not found MsgBox "Not Found" Exit Sub Else 'this finds the next empty row lastrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row + 1 ws.Range("A" & lastrow) = c ws.Range("B" & lastrow) = c.Offset(, 1) ws.Range("C" & lastrow) = c.Offset(, 4) End If End Sub