how to automatically clear text if text in another cell is removed

niygint_m

New Member
Joined
Jul 23, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
i am a beginner at vba, and am building a code to automatically clear text in column J when text in either column H or I is removed.

i have this code already, but text in column J is cleared only when text in column I is removed.
when i tried removing text in column H, text in column J doesn't clear...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Long
Dim lastRow As Long

' Check if changes occurred in columns H or I
If Not Intersect(Target, Me.Range("H:I")) Is Nothing Then
Application.EnableEvents = False ' Disable events to prevent triggering again

' Find the last row with data in column H
lastRow = Me.Cells(Me.Rows.Count, "H").End(xlUp).Row

' Loop through each row to calculate the difference
For i = 1 To lastRow

' Clear the result in J if either H or I is empty
If IsEmpty(Me.Cells(i, "H").Value) Or IsEmpty(Me.Cells(i, "I").Value) Then
Me.Cells(i, "J").ClearContents

Else

' Check if both H and I have numeric values
If IsNumeric(Me.Cells(i, "H").Value) And IsNumeric(Me.Cells(i, "I").Value) Then
Me.Cells(i, "J").Value = Me.Cells(i, "H").Value - Me.Cells(i, "I").Value
Else
Me.Cells(i, "J").ClearContents ' Clear if not numeric

End If
End If
Next i
Application.EnableEvents = True ' Re-enable events
End If
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I think you are overcomplicating it. You only need to check the row(s) that were just updated, not the whole range.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Intersect(Target, Range("H:I"))
    
'   See if any cells updated in columns H or I
    If Not rng Is Nothing Then
        Application.EnableEvents = False
'       Loop through updated cells only
        For Each cell In rng
'           Check to see if updated cell is empty
            If cell = "" Then
'               Remove value from column J
                Cells(cell.Row, "J").ClearContents
            End If
        Next cell
        Application.EnableEvents = True
    End If
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,727
Messages
6,174,146
Members
452,547
Latest member
Schilling

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