有人可以告诉我,为什么我的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
答案 0 :(得分:0)
如果我理解您要在代码中尝试实现的目的是遍历Table1
范围内的单元格(来自"搜索术语报告(1)"表单),并查看是否它位于Table2
范围内(来自" MySheet"表格)。
如果找到了,则将值放在F列中的"搜索词报告(1)"表格,我正在使用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