Excel 如何根据单元格的值运行代码?

Excel 如何根据单元格的值运行代码?,excel,vba,Excel,Vba,我最近创建了一个基于for循环的代码。它读取第一列中每行的值,并完成每行的其余部分 但是,我希望根据每行第2列的值完成该行的其余部分 实际上,第二列现在的值为“OPEN”或“CLOSED”。如果单元格处于“打开”状态,我希望程序运行;如果单元格处于“关闭”状态,我希望转到下一行 你知道我该怎么做吗 以下是我目前掌握的代码: Sub StressTest() Dim index As Integer Dim dateColumn As Integer Dim portfo

我最近创建了一个基于for循环的代码。它读取第一列中每行的值,并完成每行的其余部分

但是,我希望根据每行第2列的值完成该行的其余部分

实际上,第二列现在的值为“OPEN”或“CLOSED”。如果单元格处于“打开”状态,我希望程序运行;如果单元格处于“关闭”状态,我希望转到下一行

你知道我该怎么做吗

以下是我目前掌握的代码:

Sub StressTest()

    Dim index As Integer
    Dim dateColumn As Integer
    Dim portfolioDate As String
    Dim portfolioName As Variant
    Dim ParametricVar As Double
    Dim AuM As Double
    Dim PreviousVar As Double
    Dim PreviousAuM As Double
    Dim strPath As String
    Dim strFilePath As String
    Dim wb As Workbook
    Dim sheet As Worksheet
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim rng1 As Range
    Dim rng2 As Range
    Dim workB As Workbook
    Dim min As Double
    Dim max As Double


    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 "Current portfolio date is: " & portfolioDate




      For index = 3 To 37
    If sheet.Cells(index, 3) = "OPEN" Then
                dateColumn = MatchHeader(portfolioDate)
                portfolioName = ActiveSheet.Range("B" & 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")
                PreviousVar = sheet.Cells(index, dateColumn + 7).Value
                AuM = Workbooks(portfolioName).Worksheets("Holdings - Main View").Range("E11")
                PreviousAuM = sheet.Cells(index, dateColumn + 9).Value


                Set ws1 = wb.Worksheets("Scenarios - Main View")
                Set ws2 = wb.Worksheets("VaR - Main View")
                Set rng2 = ws2.Range("J16:J1000")
                Set rng1 = ws1.Range("11:11")

                max = Application.WorksheetFunction.max(rng2)

                sheet.Cells(index, dateColumn).Value = ParametricVar / AuM
                sheet.Cells(index, dateColumn + 2).Value = AuM
                sheet.Cells(index, dateColumn + 4).Value = ws1.Cells(10, Worst(rng1))
                sheet.Cells(index, dateColumn + 5).Value = Loss(rng1)
                sheet.Cells(index, dateColumn + 6).Value = max
                sheet.Cells(index, dateColumn + 1).Value = (ParametricVar - PreviousVar) / PreviousVar
                sheet.Cells(index, dateColumn + 3).Value = (AuM - PreviousAuM) / PreviousAuM





                wb.Close Savechanges:=False

        Next index

        Else: If sheet.Cells(index, 3) = "CLOSED" Then Next index

End Sub

迭代的结构应该如下所示:

'First bit of code
For index = 3 To 37
    If Sheet.Cells(index, 3) = "OPEN" then
        'Code
    End if
Next index
'Continue your code

请添加到目前为止您拥有的代码。如果您向我们展示您的代码,我们将能够提供帮助。可能值得一读:是什么阻碍了您使用简单的
If
-语句?我知道如何从If语句开始,但是,如果单元格值是“关闭”的,我不确定如何转到下一个索引,如
If Cells(index,2)“closed”这样的简单行,然后检查值,如果与
Closed
不同,它将继续代码,如果等于
Closed
它将跳过代码的其余部分并转到下一次迭代。