Home » excel » Excel – How do I ensure that when a value is entered into a cell, another is then blanked(and vice versa)?

Excel – How do I ensure that when a value is entered into a cell, another is then blanked(and vice versa)?

Posted by: admin April 23, 2020 Leave a comment

Questions:

So I have two columns in an excel spreadsheet – only one of those columns should be allowed to have a value in it. Lets call them Cells A1 and B1.

I figure that I can facilitate this one of two ways:

  1. When cell A1 is changed it blanks out B1

    I can do this but then when it blanks out B1, it then fires off the same event and both cells get cleared.

  2. If one of the cells has a value in it, then the other is somehow locked for editing until you clear the first cell.

Any tips would be greatly appreciated.

Thanks team!

How to&Answers:

You can disable events to avoid clearing both cells; ie

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Set rng1 = Intersect(Target, Range("A1,C1"))
If rng1 Is Nothing Then Exit Sub
Application.EnableEvents = False
If rng1.Address(0, 0) = "A1" Then
[c1].ClearContents
Else
[a1].ClearContents
End If
Application.EnableEvents = True
End Sub