If Cell value(s) in a range = "N/A" enter text "N/A" in the cell to the immediate left and add formatting

rdoulaghsingh

Board Regular
Joined
Feb 14, 2021
Messages
105
Office Version
  1. 365
Platform
  1. Windows
I have a range of cells C3:C13 and C18:C28 with dropdown lists. What I'm trying to accomplish is...if the user drops down a value of "N/A", then the cell to immediate left is locked from being edited and the cell shaded. This is what I have so far in the worksheet_change event, but it doesn't seem to work.

VBA Code:
Application.EnableEvents = False

If Target.Range("C3:C13") Or Target.Range ("C18:C28") Then
        If Target = "N/A" Then
            Target(0,1) = "(N/A)"
            With Selection.Interior
                .Pattern = xlLightDown
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
        Else
            Exit Sub
        End If
    End If

Application.EnableEvents = True

Please help!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 Then
        If Not Intersect(Target, Range("C3:C13,C18:C28")) Is Nothing Then
            Application.EnableEvents = False
            If Target.Value = "N/A" Then
                With Target(1, 0)
                    .Value = "(N/A)"
                    With .Interior
                        .Pattern = xlLightDown
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                End With
            Else
                With Target(1, 0)
                    .Value = ""
                    With .Interior
                        .Pattern = xlPatternNone
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                End With
            End If
            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 0
Solution
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 Then
        If Not Intersect(Target, Range("C3:C13,C18:C28")) Is Nothing Then
            Application.EnableEvents = False
            If Target.Value = "N/A" Then
                With Target(1, 0)
                    .Value = "(N/A)"
                    With .Interior
                        .Pattern = xlLightDown
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                End With
            Else
                With Target(1, 0)
                    .Value = ""
                    With .Interior
                        .Pattern = xlPatternNone
                        .TintAndShade = 0
                        .PatternTintAndShade = 0
                    End With
                End With
            End If
            Application.EnableEvents = True
        End If
    End If
End Sub
This worked beautifully @AlphaFrog. Thank you so much!!! Is there any way to disable the cell so the user will not be able to click in it or enter values or trigger an event tied to the cell? Is there a different event that I can use to change the values of the cell if someone copies the value of N/A down Column Range?
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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