This code does not react: Private Sub Worksheet_SelectionChange(ByVal Target As Range)…
- User
This code does not react:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim was As Worksheet Set was = ActiveSheet
If Target = "2" Then was.Range("A3").Calculate was.Range("B3").Calculate End If
- Assistant
As an AI language model, I cannot see any "End Sub" to end the code properly. It is missing in this code. Please try adding "End Sub" at the end of the code and see if it works.
- User
With the End Sub, it still does not work. My Workbook is set to manual calculation
- Assistant
If your workbook is set to manual calculation, then you need to modify the code to force the calculation to occur after the worksheet range values have changed. This can be achieved by adding the line of code "Application.Calculate" after the Target = "2" line. Here is the updated code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim was As Worksheet Set was = ActiveSheet If Target = "$B$2" Then was.Range("A3").Calculate was.Range("B3").Calculate Application.Calculate End If End SubThis should force the calculation to occur after the range values have changed.
- User
still does not work
- Assistant
If the code is still not working, it may be due to a few other potential reasons:
-
The worksheet containing the code is not the active worksheet. Double-check that the code is located in the correct worksheet module.
-
The cell reference in the "If" statement is not correct. Verify that the cell reference to trigger the calculation is accurate.
-
Your Excel settings or security settings may be preventing the code from running. Check to make sure that Excel security settings are not too high, and that macros are enabled.
If none of these solutions work, please provide more context so that we can help you better.
-
- User
When I use Target to reference a column i.e. If Target.Column = 2, the code works. But when I use Target to reference a cell, the code does not work
- Assistant
If the code works when using Target.Column to reference a column, but not Target to reference a specific cell, it may be because the syntax used for the Target reference is incorrect. To reference a specific cell, you need to use the Address method of the Range object to extract the cell address. Here is an updated version of the code that should work for you:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim was As Worksheet Set was = ActiveSheet If Target.Address = "$B$2" Then was.Range("A3").Calculate was.Range("B3").Calculate Application.Calculate End If End SubThis code uses the Target.Address property to check if the target cell address is "2" before triggering the calculation.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.