[英]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_Change
在
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.