Forum Discussion

Michael1835's avatar
Michael1835
Copper Contributor
Nov 18, 2024

VBA code - Calculate Formula from dropdown

Hi,

I have a workbook where I have had to disable automatic calculations due to workbook being slow. 

Could somebody advise a code to calculate formulas whenever an item is selected in a dropdown? 

I have attached an example of the workbook, I want formula's to calculate when the dropdown in G2 is selected. Formula's that are needed are in columns AR, AS & AT.

 

Existing code for filtering when using the dropdowns in D2 & G2:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rgEmployee As Range
    Set rgEmployee = Me.Range("G2")

    If Not Intersect(Target, rgEmployee) Is Nothing Then

        Dim rgList As Range, str As String, n As Variant
        Set rgList = Range("C_Employee_List")
        str = rgEmployee.Value
        n = Application.Match(str, rgList, 0)

        If IsError(n) Then
            rgList.EntireColumn.Hidden = False
        Else
            rgList.EntireColumn.Hidden = True
            rgList.Cells(, n).EntireColumn.Hidden = False
        End If

        Call CheckTrainingStatus

    ElseIf Not Intersect(Target, Me.Range("D2")) Is Nothing Then

        Call CheckTrainingStatus

    End If

End Sub

Sub CheckTrainingStatus()

    Dim tbl As ListObject, x As Long, y As Long, z As Long
    Set tbl = Me.ListObjects("Comp_Table")
    x = tbl.ListColumns("Refresh Training Records").Index
    y = tbl.ListColumns("Obsolete Training Records").Index
    z = tbl.ListColumns("All Training Records").Index

    With tbl.Range
        Select Case Me.Range("D2").Value
            Case "Refresh"
                .AutoFilter Field:=x, Criteria1:="<>0"
                .AutoFilter Field:=y
                .AutoFilter Field:=z
            Case "Obsolete"
                .AutoFilter Field:=x
                .AutoFilter Field:=y, Criteria1:="<>0"
                .AutoFilter Field:=z
            Case "All"
                .AutoFilter Field:=x
                .AutoFilter Field:=y
                .AutoFilter Field:=z, Criteria1:="<>0"
            Case Else 'blank
                .AutoFilter Field:=x
                .AutoFilter Field:=y
                .AutoFilter Field:=z
        End Select
    End With

End Sub

  • Above the End Sub line of CheckTrainingStatus:

        Me.Calculate

    or if you only want to recalculate columns AR to AT:

        Me.Range("AR:AT").Calculate

  • Above the End Sub line of CheckTrainingStatus:

        Me.Calculate

    or if you only want to recalculate columns AR to AT:

        Me.Range("AR:AT").Calculate

Resources