[英]vba/excel - populate cells in sheet1 from values in sheet2 based on user input into cells on sheet1
[英]VBA Vlookup to populate values from sheet1 to sheet2
有人可以告诉我为什么我的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
如果我了解您要在代码中实现的目标,就是遍历Table1
范围中的单元格(来自“搜索项报告(1)”表),并查看它是否位于Table2
Range中(来自“ MySheet”表中)。
如果找到它,则将该值放在“搜索项报告(1)”表的F列中,因为我正在使用Cl.Offset(, -3)
。
另外,如果Application.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.