简体   繁体   中英

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

I am currently working on a VBA code where you enter a date in the input box under the following form: YYYY-MM.

I then use a function that finds the cell whose value matches that of the input box and copy information in the rows below the cell with said date. Each 7 columns are used for the data of one date (1st column is the value at risk, 2nd row is the variation of value at risk, 3rd column is the market value, 4th is the variation in market value, etc) and each row represents a different portfolio. The data for each portfolio is copied from other workbooks.

The next 7 columns are for the data of the previous month.

I now want to calculate the variation in value at risk from one month to another.

To do so, I need to find the date of the month that came before the date entered in the input box and copy the value at risk from that column, assign it to a variable, and finally find the variation thanks to the formula : (current value at risk - previous value at risk) / (previous value at risk). Same goes for the variation in market value.

I have no idea how to do this as I am very new to VBA. Any help would be much appreciated. Here is what I have so far:

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

Personally I'd leave your portfolioDate as a string and then check the content of whatever is returned (I've renamed the InputBox variable to portfolioInputBox ). I've placed all this inside a Do Loop so that if the user doesn't cancel or press ok with the default input ("Type Here") then it will keep opening until a valid date is entered.

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)

The If statement is there to test whether the user has just clicked ok with the default text or has clicked cancel either of which will exit the Sub

I would then use another variable in which I would convert the portfolioInputBox input into a date format

Dim portfolioDate as Date
portfolioDate = cDate(portfolioInputBox)

I would then use this variable to calculate the previous month or handle it in one line

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM