簡體   English   中英

在輸入框中輸入日期並在 VBA 上查找上個月的日期

[英]Enter a date in input box and find the date for the previous month on VBA

我目前正在處理 VBA 代碼,您可以在輸入框中按以下格式輸入日期:YYYY-MM。

然后我使用一個函數來查找值與輸入框匹配的單元格,並在具有所述日期的單元格下方的行中復制信息。 每7列用於一個日期的數據(第一列是風險價值,第二行是風險價值的變化,第三列是市場價值,第四是市場價值的變化,等等)和每一行代表不同的投資組合。 每個投資組合的數據是從其他工作簿復制的。

接下來的 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

就我個人而言,我會將您的portfolioDate保留為字符串,然后檢查返回的任何內容(我已將InputBox變量重命名為portfolioInputBox InputBox )。 我已將所有這些放在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輸入portfolioInputBox輸入轉換為日期格式

Dim portfolioDate as Date
portfolioDate = cDate(portfolioInputBox)

然后我會使用這個變量來計算上個月或在一行中處理它

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

暫無
暫無

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

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