【问题标题】:Results depending if value found excel vba结果取决于是否找到值excel vba
【发布时间】:2016-05-06 10:45:02
【问题描述】:

我有 4 列的名称列表。所有列都来自不同的 excel 文件。我希望 E 列中单元格的值在 Y 或 N 中发生变化,具体取决于是否在其他列 A、B、C 中找到了 D 列中的值:

-Y: (if D is found in A) 
-N: (if D is found in A and B) or (if D is found in C) or (if D is not found in A and B and C)

这是我到现在为止的:

Sub find_if_in_a_and_b()
Dim FindString As String
Dim Rng As Range
Findcell = Sheets("Sheet1").Range("D:D")
If Trim(Findcell) <> "" Then
    With Sheets("Sheet1").Range("A:B")
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing Then
            Application.Goto Rng, True
        Else
            MsgBox "Nothing found"
        End If
    End With
End If
End Sub
Sub find_if_in_a_and_b_and_c()
Dim FindString As String
Dim Rng As Range
Findcell = Sheets("Sheet1").Range("D:D")
If Trim(Findcell) <> "" Then
    With Sheets("Sheet1").Range("A:C")
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing Then
            Application.Goto Rng, True
        Else
            MsgBox "Nothing found"
        End If
    End With
End If
End Sub

欢迎任何帮助。谢谢。

【问题讨论】:

  • 你需要使用VBA吗?这看起来像是一个简单的 countif 公式
  • 我同意@Phil。这可以使用公式来完成。除非我误解了你的问题。
  • 嗨,我尝试过使用 count if、条件格式、5 if's ... 但它不起作用。
  • 您要针对 A1,B1,C1 还是针对所有列 A,B,C来测试 D1 > ??
  • 嗨,案例 1:在 A:B 范围内搜索 D1,如果未找到,则在 C:C 范围内搜索。如果找不到 E1 显示 Y 。案例 2:在 A:A 范围内搜索 D1 - 如果找到,则 E1 显示 N 。

标签: vba excel


【解决方案1】:

就像朋友说的,你不需要 VBA。用这个公式COUNTIF(A:A;$D2) 用“在 A 中找到”“在 B 中找到”“在 C 中找到”创建 3 个辅助列,并检查 Y 或 N。检查此图像

【讨论】:

  • 嗨,我就是这样做的,但是...:如果他在 A 和 B C 或中找到 D2,H 列应该显示 Y b> 在 A 和 B 和 C 中找不到。如果他只在 A 中找到 D2,它应该显示 N。
  • =IF(OR(AND(E2=1;F2=1;C2=0);AND(E2=0;F2=0;C2=0));"Y";"N ")
猜你喜欢
  • 1970-01-01
  • 2018-01-22
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
相关资源
最近更新 更多