宏更改数据验证时重新计算

时间:2018-10-11 20:50:16

标签: excel vba

我有一系列的数据验证字段,我正在使用宏来回切换。

当宏设置数据验证字段的值时,工作簿将不会通过更新工作表来赶上。

我已经检查过计算是否设置为自动。每次选择后,我都尝试过重新计算工作表。

Sub generate_indications()
    ThisWorkbook.Worksheets("Filing Indication Selections").Range("H:K").ClearContents
    Application.ScreenUpdating = True
    Dim filing_sheet As Worksheet
    Dim selection_sheet As Worksheet
    Dim indication As Double
    Set filing_sheet = ThisWorkbook.Worksheets("Filing Indication")
    Set selection_sheet = ThisWorkbook.Worksheets("Filing Indication Selections")
    For Each ind_option In selection_sheet.Range("A:A")
        'filing_sheet.EnableCalculation = False
        If selection_sheet.Cells(ind_option.Row, 1) = "" Then
            'filing_sheet.EnableCalculation = True
            Exit Sub
        End If
        With filing_sheet
            .Range("A7").Value = selection_sheet.Cells(ind_option.Row, 1).Value 'Capped/Uncapped
            If selection_sheet.Cells(ind_option.Row, 2).Value = "N/A" Then
                'Set to 250K by default, though this won't affect the calculation
                .Range("A6").Value = "250K"
            Else
                .Range("A6").Value = selection_sheet.Cells(ind_option.Row, 2).Value '250K or 100K
            End If
            .Range("A8").Value = selection_sheet.Cells(ind_option.Row, 3).Value 'Paid/Incurred
            If selection_sheet.Cells(ind_option.Row, 4).Value = "CW Loss Trend" Then
                'Select the CW version
                .Range("A9").Value = selection_sheet.Cells(ind_option.Row, 4).Value
            Else
                .Range("A9").Value = .Range("L7").Value & " Loss Trend" 'Select the State Loss Trend
            End If
            .Range("A10").Value = selection_sheet.Cells(ind_option.Row, 5).Value 'Bureau/Farmers LDF
            If selection_sheet.Cells(ind_option.Row, 5).Value = "Bureau LDF" Then
                'Choose option for 2yr vs 5yr
                If Left(selection_sheet.Cells(ind_option.Row, 6).Value, 1) = 2 Then
                    .Range("A18").Value = "2 Yr Bureau"
                Else
                    .Range("A18").Value = "5 Yr Bureau"
                End If

            Else
                'Choose option 1-5 yr
                If Left(selection_sheet.Cells(ind_option.Row, 6).Value, 1) = 1 Then
                    .Range("A12").Value = "1 Yr Ave"
                ElseIf Left(selection_sheet.Cells(ind_option.Row, 6).Value, 1) = 2 Then
                    .Range("A12").Value = "2 Yr Ave"
                ElseIf Left(selection_sheet.Cells(ind_option.Row, 6).Value, 1) = 3 Then
                    .Range("A12").Value = "3 Yr Ave"
                ElseIf Left(selection_sheet.Cells(ind_option.Row, 6).Value, 1) = 4 Then
                    .Range("A12").Value = "4 Yr Ave"
                ElseIf Left(selection_sheet.Cells(ind_option.Row, 6).Value, 1) = 4 Then
                    .Range("A12").Value = "5 Yr Ave"
                Else
                    .Range("A12").Value = "Default"
                End If
            End If
            If selection_sheet.Cells(ind_option.Row, 7).Value = "CW Compliment" Then
                .Range("A11").Value = "CW LT Compliment"
            ElseIf selection_sheet.Cells(ind_option.Row, 7).Value = "Standard" Then
                .Range("A11").Value = "Standard Compliment"
            Else
                .Range("A11").Value = .Range("L7").Value & " LT Compliment"
            End If
            'filing_sheet.EnableCalculation = True
            .Calculate
            indication = .Range("I41").Value
            complement = .Range("I40").Value
            credibility = .Range("I39").Value
            preZ = .Range("I38").Value
        End With
        selection_sheet.Cells(ind_option.Row, 8).Value = indication
        selection_sheet.Cells(ind_option.Row, 9).Value = complement
        selection_sheet.Cells(ind_option.Row, 10).Value = credibility
        selection_sheet.Cells(ind_option.Row, 11).Value = preZ

    Next ind_option
End Sub

1 个答案:

答案 0 :(得分:1)

此指向类似问题的链接可能有帮助,也可能没有帮助。

Excel VBA - worksheet.calculate doesn't calculate all of my formulas

在代码中没有看到您在定义活动工作表的地方,也没有看到循环遍历每个活动工作表进行计算的循环。