[英]VBA Vlookup to populate values from sheet1 to sheet2
Someone can tell me please why is my Func doesn't write to cells nothing? 有人可以告诉我为什么我的Func不写任何东西到单元格吗? There is no error just nothing happening.
没有错误,什么也没有发生。
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
If I understood what you are trying to achieve in your code is to loop through cells in Table1
Range (from "Search term report (1)" sheet), and to see if it's found in Table2
Range (from "MySheet" sheet). 如果我了解您要在代码中实现的目标,就是遍历
Table1
范围中的单元格(来自“搜索项报告(1)”表),并查看它是否位于Table2
Range中(来自“ MySheet”表中)。
If it's found, then put the value in Column F in "Search term report (1)" sheet, for that I am using the Cl.Offset(, -3)
. 如果找到它,则将该值放在“搜索项报告(1)”表的F列中,因为我正在使用
Cl.Offset(, -3)
。
Also, there is a need to handle errors in case Application.VLookup
doesn't find a value. 另外,如果
Application.VLookup
找不到值,则需要处理错误。
Code 码
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.