VBA中的偏移功能

时间:2016-05-16 08:53:29

标签: excel-vba vba excel

Dim temp_match As Single

Dim mid_value As Single
Dim offset1 As Range
Dim offset2 As Range

Dim off_val1 As Range
Dim off_val2 As Range

temp_match = Application.Match(temp_time, Range("B2:B20"), 1) - 1

Set offset1 = Workbooks("Zero Curve").Worksheets("Sheet1").Range("B2:B20")
Set offset2 = Workbooks("Zero Curve").Worksheets("Sheet1").Range("D1:D20")

off_val1 = Evaluate("Offset(offset1, temp_match, 0,2)")
off_val2 = Evaluate("Offset(offset2, temp_match, 0,2)")

mid_value = Application.Forecast(temp_time, off_val2, off_val1)

此代码显示错误:

  

“对象变量或未设置块变量”。

请帮忙。

1 个答案:

答案 0 :(得分:0)

那里有一些问题。首先,您必须使用Set来分配对象变量。其次,你不能像这样引用公式字符串中的VBA变量。第三,那里不需要Evaluate

Set offset1 = Workbooks("Zero Curve").Worksheets("Sheet1").Range("B2:B20")
Set offset2 = Workbooks("Zero Curve").Worksheets("Sheet1").Range("D1:D20")

Set off_val1 = offset1.Offset(temp_match, 0).Resize(2)
Set off_val2 = offset2.Offset(temp_match, 0).Resize(2)