VBA - Clear one cell if another one changes in the same row

Paula03

New Member
Joined
May 22, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hey everybody,

1720086314226.png

The goal is to clear the cell in column G if I put an x in J.
(I have another sheet where all workers are listed and it's supposed to show a worker as busy if he is mentioned in this sheet. Have problems with this one too, but thats for another day🥲)

I've been working with the following code:

'** Change Cell Contens in column G if column J changes
If Not Intersect(Target, Range("J3:J100")) Is Nothing Then
Application.EnableEvents = False
Range("G3:G100").ClearContents
Application.EnableEvents = True
End If
End Sub

It works, but I have a problem.
If I make a change in any cell of column J it clears all cells in column G.
However I only want the clearing of a cell in G to happen in the specific row where the cell in J is changed. Not the whole column.
I hope this makes sense. :)

Thanks in advance!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You said " if I put an x in J"
But your code does not say x.
This script runs if you change the cell value.

Try this:
Excel Formula:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("J3:J100")) Is Nothing Then
Target.Offset(, -3).Value = ""
End If
End Sub
 
Upvote 1
You said " if I put an x in J"
But your code does not say x.
This script runs if you change the cell value.

Try this:
Excel Formula:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("J3:J100")) Is Nothing Then
Target.Offset(, -3).Value = ""
End If
End Sub
Hello,

please excuse the late reply.
I didn't put the x in the original code, because it was supposed to react no matte what I put in J.

Thank you fot the help. :)
Your code works, but now it clears both cell G and J in the row if i put an x in J.
The thing is, I need the x I put in cell J to stay for conditional formatting and other formulas.

I only want to clear cell G.

Do you happen to know why it now clears both cells?

Thanks a lot in advance!!! :D
 
Upvote 0
Your code works, but now it clears both cell G and J in the row if i put an x in J.
The thing is, I need the x I put in cell J to stay for conditional formatting and other formulas.
There is nothing in the code they posted that would clear the "x" in column J.

If it is being cleared, I suspect that you mayhave some other VBA code that is doing that.
Do you have any other VBA code on this sheet? If so, could you please post it?

Or, is this part of a table?
If so, does column J have any formulas in it, or are all the values hard-coded?
 
Upvote 1
There is nothing in the code they posted that would clear the "x" in column J.

If it is being cleared, I suspect that you mayhave some other VBA code that is doing that.
Do you have any other VBA code on this sheet? If so, could you please post it?

Or, is this part of a table?
If so, does column J have any formulas in it, or are all the values hard-coded?
Hello,

thanks for your input.

This is the whole code for the sheet:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'** Mehrfachauswahl über DropDown-Liste (Gültigkeitsprüfung)
'** Einfügen im Code-Container des betreffenden Arbeitsblattes
 
'** Dimensionierung der Variablen
Dim rngDV As Range
Dim wert_old As String
Dim wertnew As String
 
'** Errorhandling
On Error GoTo Errorhandling
 
'** Mehrfachauswahl im definierten Bereich (Bsp. B4:B14) durchführen
If Not Application.Intersect(Target, Range("G3:G100")) Is Nothing Then
 
  '**Range definieren
  Set rngDV = Target.SpecialCells(xlCellTypeAllValidation)
  If rngDV Is Nothing Then GoTo Errorhandling
  
  '** Prüfen, ob eine gültige Zelle ausgewählt wurde und Werte eintragen
  If Not Application.Intersect(Target, rngDV) Is Nothing Then
    Application.EnableEvents = False
    wertnew = Target.Value
    Application.Undo
    wertold = Target.Value
    Target.Value = wertnew
    If wertold <> "" Then
      If wertnew <> "" Then
        Target.Value = wertold & ", " & wertnew
      End If
    End If
  End If
  Application.EnableEvents = True
End If

Errorhandling:
Application.EnableEvents = True

'** Change Cell Contens in column G if column J changes
If Not Intersect(Target, Range("J3:J100")) Is Nothing Then
Target.Offset(, -3).Value = ""
 End If
End Sub

The larger part of the code is to allow multiple selection from a drop down in column G.
There are no formulas in J.

It's all part of a table.
The table will be used to appoint tasks/to dos to workers.

There is a separate sheet where it shows if a worker is "busy" with a task or "free" and can get a new task.
That is why I want to clear G if a task is done, so it shows the worker is free for the next task.

Heres a pic of the table:
1723110217582.png


I hope this helps.

I really appreciate you guys hepling!
It helps me a lot at getting better with VBA and Excel in general. :)
 
Upvote 0
Can you try moving the Errorhandling to the end of the code and making the other changes to the new code that I have here:

VBA Code:
'** Change Cell Contens in column G if column J changes
If Not Intersect(target, Range("J3:J100")) Is Nothing Then
    If LCase(target.Value) = "x" Then
        Application.EnableEvents = False
        target.Offset(, -3).Value = ""
        Application.EnableEvents = True
    End If
End If

Exit Sub

Errorhandling:
Application.EnableEvents = True
 
Upvote 1
Solution
Can you try moving the Errorhandling to the end of the code and making the other changes to the new code that I have here:

VBA Code:
'** Change Cell Contens in column G if column J changes
If Not Intersect(target, Range("J3:J100")) Is Nothing Then
    If LCase(target.Value) = "x" Then
        Application.EnableEvents = False
        target.Offset(, -3).Value = ""
        Application.EnableEvents = True
    End If
End If

Exit Sub

Errorhandling:
Application.EnableEvents = True
It worked, exept I had to change the line If LCase(target.Value) = "x" Then to If Target.Value = "x" Then.

Thank you very much.
@Alex Blakenburg @Joe4 @My Aswer Is This y'all rock!😄
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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