Share via

VBA code for Conditionally call macros.

Anonymous
2024-09-10T09:37:50+00:00

How to Conditionally call macros and VBA code for it.

Condition: If "Link Sheet" cell D4 value is equals to "FILTER DATA" call LS_FilterData macro else call LS_ClearFilter macro.

Can any body help to write VBA code for this task and where to insert the code.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2024-09-10T19:08:35+00:00

Change the code to

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$D$4" Then
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        If Range("D4").Value = "FILTER DATA" Then
            Call LS_FilterData
        Else
            Call LS_ClearFilter
        End If
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2024-09-10T13:07:38+00:00

Do you want the code to run automatically when the user changes the value of 'Link Sheet'!D4?

If so, right-click the sheet tab of Link Sheet, and select 'View Code' from the context menu.

Copy the following code into the worksheet module of Link Sheet:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("D4"), Target) Is Nothing Then
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        If Range("D4").Value = "FILTER DATA" Then
            Call LS_FilterData
        Else
            Call LS_ClearFilter
        End If
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
End Sub

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-09-10T16:06:10+00:00

    Thank you, I did what you have suggested. yes, I want to automate it. After entering your code only one macro is executing. Please be informed that my cell D4 value automatically changes i.e when I am clicking D4 cell with value "FILTER DATA" >> LS_FilterData macro running successfully and after that cell value in D4 is changing with data "CLEAR FILTER". Is this the key issue ? your code is not automating the task. Or one more thing I have to tell you>>>I have not assigned both macros to Cell D4 >> I could assign only one macro either LS_FilterData or LS_ClearFilter. Suggest me what to do?

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2024-09-10T11:31:56+00:00

    Thank you HansV, Can you please tell me where and in which sheet ,module I have to enter this code.

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2024-09-10T11:15:54+00:00

    Sub Test() If Worksheets("Link Sheet").Range("D4").Value = "FILTER DATA" Then Call LS_FilterData Else Call LS_ClearFilter End If End Sub

    Was this answer helpful?

    0 comments No comments