【问题标题】:Enter a date in input box and find the date for the previous month on VBA在输入框中输入日期并在VBA上找到上个月的日期
【发布时间】:2019-06-25 13:42:44
【问题描述】:

我目前正在处理 VBA 代码,您可以在输入框中输入日期,格式如下:YYYY-MM。

然后我使用一个函数来查找其值与输入框的值匹配的单元格,并在具有所述日期的单元格下方的行中复制信息。 每 7 列用于一个日期的数据(第 1 列是风险价值,第 2 行是风险价值的变化,第 3 列是市场价值,第 4 列是市场价值的变化等)和每一行代表不同的投资组合。每个投资组合的数据都是从其他工作簿复制而来的。

接下来的 7 列是上个月的数据。

我现在想计算一个月到另一个月的风险价值变化。

为此,我需要找到在输入框中输入的日期之前的月份日期,并从该列复制有风险的值,将其分配给变量,最后通过公式:(当前风险值 - 之前的风险值)/(之前的风险值)。市场价值的变化也是如此。

我不知道该怎么做,因为我对 VBA 很陌生。任何帮助将非常感激。这是我到目前为止所拥有的:

Option Explicit

Function MatchHeader(strSearch As String) As Long
    Dim myRight As Long, Colcount As Long
    myRight = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column

    For Colcount = 1 To myRight
        If ActiveSheet.Cells(1, Colcount) = strSearch Then
            MatchHeader = Colcount
            Exit For
        End If
    Next Colcount
End Function


Sub StressTest()
    Dim index As Integer
    Dim dateColumn As Integer
    Dim portfolioName As Variant
    Dim portfolioDate As String
    Dim ParametricVar As Double
    Dim AuM As Double
    Dim strPath As String
    Dim strFilePath As String
    Dim wb As Workbook
    Dim sheet As Worksheet

    Set wb = ThisWorkbook
    Set sheet = ActiveSheet

    portfolioDate = InputBox("Please enter date under the following form : YYYY-MM", "Date at the time of Stress Test", "Type Here")
    Debug.Print "InputBox provided value is: " & portfolioDate

    For index = 3 To Cells(Rows.Count, "A").End(xlUp).Row
        dateColumn = MatchHeader(portfolioDate)
        portfolioName = ActiveSheet.Range("A" & index & "").Value

        strPath = "G:\Risk\Risk Reports\VaR-Stress test\" & portfolioDate & "\" & portfolioName & ""

        Set wb = Workbooks.Open(strPath)

        ParametricVar = Workbooks(portfolioName).Worksheets("VaR Comparison").Range("B19")
        AuM = Workbooks(portfolioName).Worksheets("Holdings - Main View").Range("E11")

        sheet.Cells(index, dateColumn).Value = ParametricVar / AuM
        sheet.Cells(index, dateColumn + 2).Value = AuM
        sheet.Cells(index, dateColumn + 5).Value = Application.WorksheetFunction.Min(Workbooks(wb).Worksheets("VaR Comparison").Range("P11:AA11"))
        sheet.Cells(index, dateColumn + 6).Value = Application.WorksheetFunction.Max(Workbooks(wb).Worksheets("VaR Comparison").Range("J16:J1000"))

        wb.Close Savechanges:=False
    Next index
End Sub

【问题讨论】:

标签: excel vba


【解决方案1】:

我个人会将您的portfolioDate 保留为字符串,然后检查返回的内容(我已将InputBox 变量重命名为portfolioInputBox)。我将所有这些都放在了 Do 循环中,这样如果用户没有取消或使用默认输入(“在此处输入”)按确定,那么它将一直打开,直到输入有效日期。

Dim portfolioInputBox As String

Do
    portfolioInputBox = InputBox("Please enter date under the following form : YYYY-MM", "Date at the time of Stress Test", "Type Here")

    If portfolioInputBox = "Type Here" Or portfolioInputBox = vbNullString Then Exit Sub
Loop Until IsDate(portfolioInputBox)

If 语句用于测试用户是使用默认文本单击确定还是单击取消,其中任何一个都将退出 Sub

然后,我将使用另一个变量,将portfolioInputBox 输入转换为日期格式

Dim portfolioDate as Date
portfolioDate = cDate(portfolioInputBox)

然后我会使用这个变量来计算上个月或在一行中处理它

portfolioDate = DateAdd("M", -1, CDate(portfolioInputBox))

【讨论】:

    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多