如何编写激活不同工作表的VBA功能?

时间:2018-12-28 10:42:48

标签: excel vba excel-vba

我正在使用一个输入来编写VBA函数,该输入确定包含其他输入的工作表。

对于不同的曲线名称,该函数应引用不同图纸中的数据。我的代码如下:

Public Function DFrate(mtmdate As Date, pmtdate As Date, curvename As String, colno As Integer) As Double

Dim yf As Double
Dim noday As Integer
Dim lastrow As Integer
Dim rate As Range
Dim tenor As Range
Dim DFinv As Double
Dim DFinv1 As Double
Dim DFinv2 As Double

noday = pmtdate - mtmdate
yf = noday / 360
MsgBox noday

ThisWorkbook.Sheets("HS_" & curvename).Activate
lastrow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
Set rate = ActiveSheet.Range(Cells(102, 3 + colno), Cells(lastrow, 3 + colno))
Set tenor = ActiveSheet.Range(Cells(102, 2), Cells(lastrow, 2))

If (noday <= tenor(1, 1)) Then

     DFinv1 = (1 + rate(1, 1) / 100) ^ yf
     DFinv2 = (1 + rate(2, 1) / 100) ^ yf
     DFinv = DFinv1 + (noday - tenor(1, 1)) * (DFinv2 - DFinv1) / (tenor(2, 1) - tenor(1, 1))
     MsgBox DFinv

End If
For k = 1 To lastrow

     If (noday > tenor(k, 1) And noday <= tenor(k + 1, 1)) Then
            DFinv1 = (1 + rate(k, 1) / 100) ^ (tenor(k, 1) / 360)
            DFinv2 = (1 + rate(k + 1, 1) / 100) ^ (tenor(k + 1, 1) / 360)
            DFinv = DFinv1 + (noday - tenor(k, 1)) * (DFinv2 - DFinv1) / (tenor(k + 1, 1) - tenor(k, 1))

            Exit For
     End If
    Next k
DFrate = DFinv

End Function

我收到了错误#NAME? 甚至“ Msgbox noday”消息框也不起作用。

有人可以让我知道我的代码应该更改什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

如果我是

  • 将以下代码(与您的代码稍有不同)放入常规模块(不是Thisworkbook或任何Sheet模块中),
  • 创建一个名为"HS_O"的工作表,
  • 5放入工作表B102的单元格"HS_O"中,将3放入工作表D102的单元格"HS_O"
  • 并将=DFrate(TODAY(),TODAY(),"O",1)放在Thisworkbook内任何工作表的任何单元格中

我得到的返回值为1。我认为它对我有用(理论上也应该对您有用)。

Option Explicit

Public Function DFrate(mtmdate As Date, pmtdate As Date, curvename As String, colno As Long) As Double

    Dim yf As Double
    Dim noday As Long
    Dim lastrow As Long
    Dim rate As Range
    Dim tenor As Range
    Dim DFinv As Double
    Dim DFinv1 As Double
    Dim DFinv2 As Double
    Dim k As Long

    noday = pmtdate - mtmdate
    yf = noday / 360

    ' Maybe have a defensive check/guard
    ' or some return particular return value if sheet doesn't exist
    With ThisWorkbook.Sheets("HS_" & curvename)
        lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set rate = .Range(.Cells(102, 3 + colno), .Cells(lastrow, 3 + colno))
        Set tenor = .Range(.Cells(102, 2), .Cells(lastrow, 2))
    End With

    If (noday <= tenor(1, 1)) Then
         DFinv1 = (1 + rate(1, 1) / 100) ^ yf
         DFinv2 = (1 + rate(2, 1) / 100) ^ yf
         DFinv = DFinv1 + (noday - tenor(1, 1)) * (DFinv2 - DFinv1) / (tenor(2, 1) - tenor(1, 1))
         MsgBox DFinv
    End If

    For k = 1 To lastrow
         If (noday > tenor(k, 1) And noday <= tenor(k + 1, 1)) Then
            DFinv1 = (1 + rate(k, 1) / 100) ^ (tenor(k, 1) / 360)
            DFinv2 = (1 + rate(k + 1, 1) / 100) ^ (tenor(k + 1, 1) / 360)
            DFinv = DFinv1 + (noday - tenor(k, 1)) * (DFinv2 - DFinv1) / (tenor(k + 1, 1) - tenor(k, 1))

            Exit For
         End If
        Next k
    DFrate = DFinv

End Function

我不太需要从工作表中调用UDF。也许仅仅调用函数会激活函数所在的工作表,而不是"HS_" & curvename工作表。我不确定无论哪种方式,我们都可以使用With语句。