简体   繁体   English

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

[英]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.我的表 (F -> I) 中有可能无限行的列,这些列是带有简单是/否列表的下拉列表。

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

Is this what you are trying?这是你正在尝试的吗? I have commented the code.我已经评论了代码。 For more explanation, I would recommend going through How to handle Worksheet_Change有关更多解释,我建议您阅读如何处理 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在行动

在此处输入图像描述

Try this code in the Worksheet_ChangeWorksheet_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.

相关问题 根据另一个单元格值返回一个单元格值 - Return a cell value based on another cell value 根据另一个单元格中的月份返回一个单元格值 - Return one cell value based on month in another cell Excel - 如果单元格包含列表中的文本,则返回另一个单元格的值 - Excel - if cell contains text in a list then return value of another cell 根据同一行中另一个单元格的文本颜色设置一个单元格的值 - Set value of a cell based on the text color of another cell on the same row 如何根据另一个单元格更新您输入值的同一单元格? - How do you update the same cell you've entered a value in, based on another cell? Excel宏,如果在同一行的另一个单元格中输入了一个值,则可以更改单元格的对齐方式,边框和自动换行 - Excel Macro to change cell alignment, borders and wrap text if a value has been entered into another cell in the same row 根据另一张纸上的另一个单元格值返回一行中的最后一个值 - Return last value in a row, based on another cell value on another sheet 如果下一个单元格文本与当前单元格文本匹配,则返回另一个单元格中的相邻值 - If next cell text matches current cell text then return adjacent value in another cell 根据另一个单元格中的值更改单元格中的值 - Change value in a cell based on value in another cell 根据另一个单元格的值更改单元格的值 - changing value of cell based on value of another cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM