If Then VBA Script

lilwillis3000

New Member
Joined
Apr 9, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Please help. I've been trying to come up with a VBA Script that says this

If column D equals anything but ***NAME NOT LISTED***, Then return "N/A" in that same row in column G.

I already have Data Validation to restrict editing in column G if column D is not ***NAME NOT LISTED***
1712697999429.png
 
How about this?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim changedCell As Range
    Dim intersectRange As Range

    ' Define the range of column D from D30 downwards
    Set intersectRange = Intersect(Target, Me.Range("D30:D" & Me.Rows.Count))

    If Not intersectRange Is Nothing Then
        Application.EnableEvents = False
        For Each changedCell In intersectRange
            If changedCell.Value = "" Then ' Check if the value is cleared out
                Me.Cells(changedCell.Row, "G").ClearContents ' Clear content in column G
            ElseIf changedCell.Value <> "***NAME NOT LISTED***" Then
                Me.Cells(changedCell.Row, "G").Value = "N/A"
            Else
                Me.Cells(changedCell.Row, "G").ClearContents
            End If
        Next changedCell
        Application.EnableEvents = True
    End If
End Sub
This worked perfectly! Thanks for the assistance!
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,215,432
Messages
6,124,860
Members
449,194
Latest member
HellScout

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