Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emptying specific cells in the same row of an amended cell

I am relatively new to VBA.

Below is my code that works on just row 2.

    Option Explicit

    Public precedent

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Me.[D2]) Is Nothing Then
            If Me.precedent <> Me.[D2].Value Then
               Me.[F2] = ""
               Me.[H2] = ""
               Me.precedent = Me.[D2].Value
            End If
        End If
    End Sub

I would like this code to run on every row except row 1 as this is my header.

How do I do this? Would I use a loop?

like image 882
Paul Young Avatar asked Dec 06 '25 02:12

Paul Young


1 Answers

It shouldn't be so complicated. Just check Target.Row and Target.Column. If the former is greater than 1 and the latter is equal to 4, trigger whatever action you want.

Modify the following code accordingly.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row > 1 And Target.Column = 4 Then
        Range("F" & Target.Row) = vbNullString
        Range("H" & Target.Row) = vbNullString
    End If
End Sub

Let us know if this helps.

like image 143
NullDev Avatar answered Dec 10 '25 10:12

NullDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!