簡體   English   中英

在 VBA excel 中使用 application.worksheetfunction.Vlookup 時出現范圍錯誤

[英]Range Error when using application.worksheetfunction.Vlookup in VBA excel

我有以下代碼:

Function MyLookup(var1 As Variant, range1 As Range, var2 As Integer) As Double
MyLookup = Application.WorksheetFunction.VLookup(var1, range1, var2, False)
End Function

我用這個function來計算下面function中的一些參數

Function Test(IntMethod As String, CFDate As Date, ValDate As Date, _
              CF As Double, RepFreq As Integer, DiscToDate As String, _
              DiscType As String) As Double
Dim InterpPeriod As Double, PrevDateRate As Double, _
    CurrDateRate As Double, PrevCurvMat As Integer, _
    CurrCurvMat As Integer, rate As Double
Dim Yield_Curves As Range, PrevValDate As Date

Set Yield_Curves = DISC_CFS.Range("Yield_Curves")
PrevValDate = Last_Period(ValDate, CFDate, RepFreq)
'-----------------------------------------------------
InterpPeriod = Application.WorksheetFunction.YearFrac(CFDate, ValDate, 1)
PrevCurvMat = YearsDiff(ValDate, CFDate)
CurrCurvMat = YearsDiff(ValDate, CFDate) + 1
PrevDateRate = MyLookup(PrevValDate, Yield_Curves, 2)
CurrDateRate = MyLookup(ValDate, Yield_Curves, 2)

當我到達 MyLookup function 時,無法識別范圍並退出 function。 有什么提示嗎?

先感謝您!

我終於設法解決了這個問題。 錯誤出現在我的一個查找變量中,它是一個日期。 日期應轉換為長類型。

正確的代碼如下:

Function MyLookup(var1 As Variant, range1 As Range, var2 As Integer) As Double
MyLookup = Application.WorksheetFunction.VLookup(var1, range1, var2, False)
End Function

對於主要代碼:

Function Test(IntMethod As String, CFDate As Date, ValDate As Date, _
              CF As Double, RepFreq As Integer, DiscToDate As String, _
              DiscType As String) As Double
Dim InterpPeriod As Double, PrevDateRate As Double, _
    CurrDateRate As Double, PrevCurvMat As Integer, _
    CurrCurvMat As Integer, rate As Double
Dim Yield_Curves As Range, PrevValDate As Date

Set Yield_Curves = DISC_CFS.Range("Yield_Curves")
PrevValDate = Last_Period(ValDate, CFDate, RepFreq)
'-----------------------------------------------------
InterpPeriod = Application.WorksheetFunction.YearFrac(CFDate, ValDate, 1)
PrevCurvMat = YearsDiff(ValDate, CFDate)
CurrCurvMat = YearsDiff(ValDate, CFDate) + 1
PrevDateRate = MyLookup(CLng(PrevValDate), Yield_Curves, 2)
CurrDateRate = MyLookup(CLng(ValDate), Yield_Curves, 2)

感謝您提供的答案和幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM