Visual Basic 的 VLOOKUP
Visual Basic VLOOKUP
我想自动 运行 以下语句:
=VLOOKUP(B8,Sheet2!D:H,5,FALSE)
但我在第 6 行遇到错误:无法获取 WorksheetFunction 的 VLookup 属性 class
Sub Test()
Dim rng As Range
Dim i As Long
With ActiveSheet
Set rng = .Range("B8:D" & .Cells(.Rows.Count, 1).End(xlUp).Row)
For i = 18 to rng.Rows.Count
rng.Cells(8, 6) =
Application.WorksheetFunction.VLookup(.Cells(i,1), Sheets("Sheet2").Range("D:H"), 5, False)
Next
End With
End Sub
通过查找 Sheet2
我想将输出放在 SheetT F8 的位置
我怀疑你想要
Sub Test()
Dim i As Long
With ActiveSheet
For i = 8 to .Cells(.Rows.Count, "B").End(xlUp).Row
.Cells(i, "F").Value = Application.VLookup(.Cells(i, "B").Value, _
Sheets("Sheet2").Range("D:H"), _
5, _
False)
Next
End With
End Sub
您的原始代码是:
- 总是写入
rng.Cells(8, 6)
,这是一个 B8
偏移 7 行和 5 列的单元格,即 G15
(或早期版本中的 C15
)
- 使用
ActiveSheet.Cells(i, 1)
作为查找值,这是 A 列中的一个单元格
我想自动 运行 以下语句:
=VLOOKUP(B8,Sheet2!D:H,5,FALSE)
但我在第 6 行遇到错误:无法获取 WorksheetFunction 的 VLookup 属性 class
Sub Test()
Dim rng As Range
Dim i As Long
With ActiveSheet
Set rng = .Range("B8:D" & .Cells(.Rows.Count, 1).End(xlUp).Row)
For i = 18 to rng.Rows.Count
rng.Cells(8, 6) =
Application.WorksheetFunction.VLookup(.Cells(i,1), Sheets("Sheet2").Range("D:H"), 5, False)
Next
End With
End Sub
通过查找 Sheet2
我想将输出放在 SheetT F8 的位置我怀疑你想要
Sub Test()
Dim i As Long
With ActiveSheet
For i = 8 to .Cells(.Rows.Count, "B").End(xlUp).Row
.Cells(i, "F").Value = Application.VLookup(.Cells(i, "B").Value, _
Sheets("Sheet2").Range("D:H"), _
5, _
False)
Next
End With
End Sub
您的原始代码是:
- 总是写入
rng.Cells(8, 6)
,这是一个B8
偏移 7 行和 5 列的单元格,即G15
(或早期版本中的C15
) - 使用
ActiveSheet.Cells(i, 1)
作为查找值,这是 A 列中的一个单元格