Clearing cells when another cell becomes populated

Lobz

New Member
Joined
Nov 12, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am very new to vba and macros and I am trying to clear cells automatically when another cell gets a value put into it. An example would be: If C3 has a value, clear B3, but I'd like it to do that to an entire table range. I've used this code:
Sub CLEARCELLS()



If Sheet1.Range("C3").Value <> "" Then

Sheet1.Range("B3").ClearContents

End If



End Sub

It works perfect for that particular cell, but I need it to cover all of column B & C and I'm pretty stuck since this is all new to me. Any help would be appreciated, thank you.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Sub leaving()
Dim barcode As String
Dim rownumber, lastrow As Long
Dim rng As Range
Dim location As String
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).row + 1
barcode = Sheet3.Cells(2, 2).Value
If Sheet3.Cells(2, 2) <> "" Then
Set rng = Sheet3.Columns("a:a").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
rownumber = rng.row


Sheet3.Cells(rownumber, 3) = Date & " " & Time
Sheet3.Cells(rownumber, 3).NumberFormat = "m/d/yyyy h:mm AM/PM"
Sheet3.Cells(2, 2).ClearContents
End If
End Sub
 
Upvote 0
Try this, with the "leaving" added to MAIT's sub
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  11/12/2022  10:11:27 PM  EST
If Target.Column = 3 Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Application.EnableEvents = False
If Target.Value <> "" Then Target.Offset(, -1).Clear
Call leaving
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Where would I put that code? I tried putting it on the same sheet and I think it broke lol. Sorry, I am new and have never coded in any way prior to maybe a month ago.
 
Upvote 0
It goes in the sheet module you want it to apply to.
Rclick on the sheet tab>>viewcode>>paste in the Rhand window. Making sure there is no other code in that window.
 
Upvote 0
Thats the problem. I have all that code on that sheet already that I shared on the previous post.
Private Sub Worksheet_Change(ByVal Target As Range)


If Not Intersect(Target, Me.Range("B2")) Is Nothing Then

Call leaving
Application.EnableEvents = True
End If

End Sub
Sub ClearCell()

If IsDate(Range("C4:C1000").Value) Then
MsgBox ("Cells")
End If

End Sub

Sub ClearLocation()

If Range("c4:c1000") <> "" Then
Range("b4:b1000").ClearContents
End If

End Sub

All of this is already on the sheet I want it to affect. Is there a way to copy that sheet so I have a clean sheet for coding? or is that something not possible?

I want column b in the table to clear once column c has a value entered into it. This way it won't keep track of that record on my next sheet which gives a better visual of the locations being tracked.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top