[英]Excel 2007 VBA - Use VLOOKUP to find value in different worksheet and store result in active sheet
I am new to this group and VBA. 我是这个小组和VBA的新手。
I have a worksheet 'T2', with a variable number of rows. 我有一个工作表'T2',行数可变。 Starting with row 3, I want to look up the item number found in column 'E' and search worksheet 'Support'.
从第3行开始,我想查找“E”列中找到的项目编号并搜索工作表“支持”。 The item number can be found somewhere in column 'A' in 'Support'.
项目编号可以在“支持”中的“A”列中的某处找到。 I need to bring the value in column 'B' in Support (that relates to that item number) and store it in column 'A' in worksheet 'T2'.
我需要将支持中的列'B'中的值(与该项目编号相关)并将其存储在工作表'T2'中的列'A'中。 I then want to repeat this process for every row in 'T2' up to and including the last row.
然后我想对'T2'中的每一行重复这个过程,直到并包括最后一行。
Can anyone help please as I just can't get this to work at all. 任何人都可以帮助,因为我根本无法让它工作。
Thanks 谢谢
For the Vlookup part, this should be what you need. 对于Vlookup部分,这应该是您需要的。
Sub with_some_name
Dim result As String
With Worksheets("T2")
result = Application.WorksheetFunction.VLookup(.Range("e3"), Worksheets("support").Range("A:B"), 2, False)
Range("A3").Value = result
End With
end sub
variant using Dictionary: 变体使用词典:
Sub Test1()
Dim Dic As Object, key As Variant, oCell As Range, i&
Dim w1 As Worksheet, w2 As Worksheet
Set Dic = CreateObject("Scripting.Dictionary")
Set w1 = Sheets("Support")
Set w2 = Sheets("T2")
i = w1.Cells(Rows.Count, "A").End(xlUp).Row
For Each oCell In w1.Range("A1:A" & i)
If Not Dic.exists(oCell.Value) Then
Dic.Add oCell.Value, oCell.Offset(, 1).Value
End If
Next
i = w2.Cells(Rows.Count, "E").End(xlUp).Row
For Each oCell In w2.Range("E3:E" & i)
For Each key In Dic
If oCell.Value = key Then
oCell.Offset(, -4).Value = Dic(key)
End If
Next
Next
End Sub
variant using vlookup: 使用vlookup的变体:
Sub Test2()
Dim oCell As Range
Dim w1 As Worksheet, w2 As Worksheet
Set w1 = Sheets("Support")
Set w2 = Sheets("T2")
i = w2.Cells(Rows.Count, "E").End(xlUp).Row
On Error Resume Next
For Each oCell In w2.Range("E3:E" & i)
oCell.Offset(, -4).Value = WorksheetFunction.VLookup(oCell.Value, w1.[A:B], 2, 0)
Next
End Sub
variant using find method: 使用find方法的变体:
Sub Test3()
Dim oCell As Range
Dim w1 As Worksheet, w2 As Worksheet
Set w1 = Sheets("Support")
Set w2 = Sheets("T2")
i = w2.Cells(Rows.Count, "E").End(xlUp).Row
On Error Resume Next
For Each oCell In w2.Range("E3:E" & i)
oCell.Offset(, -4).Value = w1.Cells(w1.Columns(1).Find(oCell.Value).Row, 2).Value
Next
End Sub
output result 输出结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.