我的代码存在问题:
代码:
Sub CPT_Click()
Dim CPTBook, PRBook As Workbook
Dim CPTSheet, PRSheet As Worksheet
Dim CPTRange As Range
Dim myResult, lookValue As String
Set PRBook = ThisWorkbook
Set PRSheet = PRBook.Worksheets("Implementation")
Set CPTBook = Workbooks.Open("CPT.xlsx", ReadOnly:=True)
Set CPTSheet = CPTBook.Worksheets(2)
Set CPTRange = CPTSheet.Range("G4:DY300")
lookValue = PRSheet.Range("U18").Value 'returns correct value
myResult = Application.WorksheetFunction.VLookup(lookValue, CPTRange, 2, False)
MsgBox myResult
End Sub
当我点击我的按钮时,我得到了着名的:“无法获取WorksheetFunction类的Vlookup属性”错误消息。
我已尝试过所有内容,当在工作表中键入Vlookup函数时,我得到了正确的值 - 所以我在CPTRange中的CPTSheet中寻找(lookValue)值。
有什么想法吗?
提前致谢。
答案 0 :(得分:0)
您需要将Address属性的CPTRange的External属性设置为true。我也会使用Evaluate方法:
myResult = Evaluate("VLookup(""" & lookValue & """," & CPTRange.Address(External:=True) & ",2,False)")