Mar 01 2024 12:59 PM
I seek to interrogate the value in a cell (integer 0 or 1), and then arbitrarily force a text value into another free text entry cell. Example. A1 has an integer value that is unknown (and not relevant to be known). B1 has a value 0, or 1. I need a formula that will force A1 to 0, if B1 is 1. If B1 is 0, A1s values arent changed, irrespective of whether they have changed or not. Since A1 is guaranteed to change dynamically, it cannot contain a formula.
Mar 01 2024 01:45 PM
This requires VBA code (so it'll work only in the desktop version of Excel for Windows and Mac).
In your example:
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the code listed below into the worksheet module.
Switch back to Excel.
Save the workbook as a macro-enabled workbook (*.xlsm).
Make sure that you allow macros when you open the workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("B1").Value, Target) Is Nothing Then
If Range("B1").Value = 1 Then
Application.EnableEvents = False
Range("A1").Value = 0
Application.EnableEvents = True
End If
End If
End Sub
P.S. This assumes that the user enters 0 or 1 in cell B1. If B1 contains a formula that returns 0 or 1, use
Private Sub Worksheet_Calculate()
If Range("B1").Value = 1 Then
Application.EnableEvents = False
Range("A1").Value = 0
Application.EnableEvents = True
End If
End Sub
Mar 01 2024 07:58 PM
Mar 01 2024 08:12 PM
Mar 01 2024 08:35 PM - edited Mar 01 2024 08:55 PM
Note this error 424 comes up no matter what value I put in E2 (0 or 1). Correction. The error comes up if I change the value in ANY cell.
Mar 02 2024 12:36 AM
My apologies, it was air code. It should have been
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("E2"), Target) Is Nothing Then
If Range("E2").Value = 1 Then
Application.EnableEvents = False
Range("D2").Value = 0
Application.EnableEvents = True
End If
End If
End Sub
Mar 02 2024 08:51 AM