我试图弄清楚如何在vba中使用vlookup。
Function Check_NPI_NUM(pracno As String, npi As String) As String
Dim tempRange As Range
Set tempRange = ThisWorkbook.Worksheets("tempSheet").Columns("C:D")
Dim fixedPracno As Variant
'fixed pracno will look like this "$C$64941"
fixedPracno = Replace(pracno, "H", "C")
If (npi = "0") Then
Check_NPI_NUM = Application.IfError(Application.VLookup(fixedPracno, tempRange, 2, False), "0")
Else
Check_NPI_NUM = npi
End If
End Function
我认为我的问题是查找另一张表的第二部分。我得到了一个不匹配类型错误,我无法弄清楚原因。如果它是查找的第一部分,有人可以解释原因吗?
答案 0 :(得分:1)
在VBA中使用Find
和Offset
而不是VLookup
更好一点。偏移一列以复制2,0)
中的VLOOKUP
。像这样:
Sub Test()
Dim tableArray As Range, found As Range
Set tableArray = ThisWorkbook.Worksheets("Sheet1").Range("A1:A1000")
Set found = tableArray.Find(what:="someValue", lookat:=xlWhole)
If Not found Is Nothing Then
MsgBox found.Offset(0, 1).Value
Else
MsgBox "Not found"
End If
End Sub