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
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Why VBA, a formula will do the same thing.
In column "G", Drag down as required.
Excel Formula:
=IF(D30<>"***NAME NOT LISTED***","N/A","")
 
Upvote 0
Why VBA, a formula will do the same thing.
In column "G", Drag down as required.
Excel Formula:
=IF(D30<>"***NAME NOT LISTED***","N/A","")
Because I still want users to fill in the unit name in Column G if it's not listed in Column D. If I do it this way, will they not write over the formula?
 
Upvote 0
Maybe this
VBA Code:
Sub MM1()
 Dim lr As Long, r As Long
 lr = Cells(Rows.Count, "D").End(xlUp).Row
 For r = 30 To lr
    If Range("D" & r).Value <> "***NAME NOT LISTED***" Then
        Range("G" & r).Value = "N/A"
    End If
Next r
    End Sub
 
Upvote 0
Maybe this
VBA Code:
Sub MM1()
 Dim lr As Long, r As Long
 lr = Cells(Rows.Count, "D").End(xlUp).Row
 For r = 30 To lr
    If Range("D" & r).Value <> "***NAME NOT LISTED***" Then
        Range("G" & r).Value = "N/A"
    End If
Next r
    End Sub

Thanks but not quite. This makes everything N/A all the way down first. I want Column G to be clear until something other than ***NAME NOT LISTED*** is selected in Column D.

Also, I have to manually run the macro each time the change is made. How can I set it to run automatically after something is selected in Column D?

1712755670018.png
 
Upvote 0
This triggers when a value from cells D30 and below are changed. This should go into your sheet VB module, not the general. Try on a copy.
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 <> "***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
 
Upvote 1
This triggers when a value from cells D30 and below are changed. This should go into your sheet VB module, not the general. Try on a copy.
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 <> "***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
Thanks but I Excel will not let me run it (bottom code). Every time I try to run it, it asks me to save the Macro Name and when I do, It starts a new line of code. What am I missing? I've compared it to the top code for a similar spreadsheet that works fine.

1712760370596.png
 
Upvote 0
Another variant to what @Cubist posted:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, R As Range
    
    Set rng = Me.Range("D30:D" & Me.Range("B" & Me.Rows.Count).End(xlUp).Row)
    
    If Not Intersect(rng, Target) Is Nothing Then
        Application.EnableEvents = False
        For Each R In rng
            If Trim(R.Value) = "***NAME NOT LISTED***" Then
                R.Offset(0, 3).Value = vbNullString
            Else
                R.Offset(0, 3).Value = "N/A"
            End If
        Next R
        Application.EnableEvents = True
    End If
End Sub

This code MUST go in a worksheet code module, not a general code module.

1712761629094.png
 
Upvote 0
This triggers when a value from cells D30 and below are changed. This should go into your sheet VB module, not the general. Try on a copy.
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 <> "***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
So this worked! Just need one minor tweak! When I select anything other than ***NAME NOT LISTED*** in Column D, N/A is populated in Column G as desired. However, if something from Column D is deleted or cleared, Column G rm

1712763182506.png
 
Upvote 0
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
 
Upvote 1
Solution

Forum statistics

Threads
1,215,430
Messages
6,124,850
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