Martin
Martin

Reputation: 1

Return text in cell based on value entered in another cell

I have columns in my table (F -> I) with potentially unlimited rows, which are drop downs with a simple Yes/No list.

It starts as an empty row and the user inputs data in other rows and then selects either Yes/No based on the questions.

What I'm looking for is some VBA to say If the user has selected 'No' in Column F, then in Column K, prepopulate with "Column F: ". The idea is that anything that is selected as "No", is populated in K so the user can add their notes and reference Column F. For example: "Column F: This did not meet requirements because xxxxx"

I have tried a few examples whilst searching the net but nothing seems to work:

R = 4

'loop to the last row
Do Until Range("F" & R) = ""
    'check each cell if if contains 'apple' then..
    '..place 'Contains Apple' on column B
    If InStr(1, Range("F" & R), "No") Then
        Range("K" & R) = "Test Plan"
    End If
    
    R = R + 1
Loop

I also tried putting that in a worksheet change sub but it didn't work as expected.

Any help appreciated. Thanks

Upvotes: 0

Views: 176

Answers (2)

Siddharth Rout
Siddharth Rout

Reputation: 149287

Is this what you are trying? I have commented the code. For more explanation, I would recommend going through How to handle Worksheet_Change

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim aCell As Range
    
    '~~> Error handling
    On Error GoTo Whoa
    
    '~~> Switch off events
    Application.EnableEvents = False
    
    '~~> Check of the change happened in Col F
    If Not Intersect(Target, Columns(6)) Is Nothing Then
        '~~> Loop through all the cells in col F where
        '~~> the change happened
        For Each aCell In Target.Cells
            '~~> Check if the value is NO
            If UCase(aCell.Value2) = "NO" Then
                '~~> Update Col K
                Range("K" & aCell.Row).Value = "Test Plan"
            Else
                '~~> If not NO then WHAT ACTION? For example user
                '~~> deletes the existing NO
            End If
        Next
    End If
    
Letscontinue:
    '~~> Switch events back on
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

In Action

enter image description here

Upvotes: 1

Glenn G
Glenn G

Reputation: 667

Try this code in the Worksheet_Change

Option Explicit
Option Compare Text

Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Column = 6 And Target.Value = "No" Then
            Target.Parent.Range("K" & Target.Row).Value = "Column F: "
        End If
End Sub

Upvotes: 0

Related Questions