繁体   English   中英

根据在另一个单元格中输入的值返回单元格中的文本

[英]Return text in cell based on value entered in another cell

我的表 (F -> I) 中有可能无限行的列,这些列是带有简单是/否列表的下拉列表。

它以空行开始,用户在其他行中输入数据,然后根据问题选择是/否。

我正在寻找的是一些 VBA 说如果用户在 F 列中选择了“否”,那么在 K 列中,预填充“F 列:”。 这个想法是,任何选择为“否”的内容都填充在 K 中,因此用户可以添加他们的注释并参考 F 列。例如:“F 列:这不符合要求,因为 xxxxx”

我在搜索网络时尝试了一些示例,但似乎没有任何效果:

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

我也尝试将它放在工作表更改子中,但它没有按预期工作。

任何帮助表示赞赏。 谢谢

这是你正在尝试的吗? 我已经评论了代码。 有关更多解释,我建议您阅读如何处理 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

在行动

在此处输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM