VBA Vlookup将值从表1填充到表2

VBA Vlookup将值从表1填充到表2,vba,excel,vlookup,Vba,Excel,Vlookup,有人能告诉我为什么我的Func不写任何东西吗? 没有错误,只是什么都没有发生 Sub VL_Depositors() On Error Resume Next Dim Dept_Row As Long Dim Dept_Clm As Long Dim Table1, Table2 As Range Worksheets("Search term report (1)").Select Set Table1 = Range("I2:I10") Worksheets("MySheet").S

有人能告诉我为什么我的Func不写任何东西吗? 没有错误,只是什么都没有发生

Sub VL_Depositors()

On Error Resume Next

Dim Dept_Row As Long
Dim Dept_Clm As Long
Dim Table1, Table2 As Range

Worksheets("Search term report (1)").Select
Set Table1 = Range("I2:I10")

Worksheets("MySheet").Select
Set Table2 = Range("E2:F39")

Worksheets("Search term report (1)").Select
Dept_Row = Sheet1.Range("F2").row
Dept_Clm = Sheet1.Range("F2").Column

For Each cl In Table1
    Cells(Dept_Row, Dept_Clm) = Application.WorksheetFunction.VLookup(cl,Table2, 2, False)
    Dept_Row = Dept_Row + 1
Next cl

End Sub

如果我了解您试图在代码中实现的目标,请在
表1
范围(来自“搜索词报告(1)”表)中的单元格中循环,并查看它是否在
表2
范围(来自“MySheet”表)中找到

如果找到了,则将该值放在“搜索词报告(1)”表的F列中,因为我使用的是
Cl.Offset(,-3)

另外,如果
应用程序.VLookup
找不到值,则需要处理错误

代码

Option Explicit

Sub VL_Depositors()

Dim Dept_Row As Long
Dim Dept_Clm As Long
Dim Table1 As Range, Table2 As Range
Dim Cl As Range

Set Table1 = Worksheets("Search term report (1)").Range("I2:I10")
Set Table2 = Worksheets("MySheet").Range("E2:F39")

For Each Cl In Table1.Cells
    If Not IsError(Application.VLookup(Cl.Value, Table2, 2, False)) Then ' <-- VLookup was successful
        ' use offset -3 columns  >> 3 columns to the left of column "I" is column "F"
        Cl.Offset(, -3).Value = Application.VLookup(Cl.Value, Table2, 2, False)
    Else
        Cl.Offset(, -3).Value = "Value not Found!"
    End If
Next Cl

End Sub
选项显式
次级VLU储户()
同长一排
Dim Dept_Clm尽可能长
调暗表1作为范围,表2作为范围
Dim Cl As范围
Set Table1=工作表(“搜索词报告(1)”)。范围(“I2:I10”)
Set Table2=工作表(“MySheet”)。范围(“E2:F39”)
对于表1中的每个Cl。单元格

如果不是IsError(Application.VLookup(Cl.Value,表2,2,False)),则“删除出错时的
,然后继续下一步
,然后告诉我们它在哪一行出错。您是否试图将
VLookup
中的值放入F列?从第2行到第10行?从第2行到第10行。所以我把偏移量改为:Cl.offset(,1).ValueHi-Shai谢谢你的帮助!您理解正确,我刚刚更改了偏移量属性。