结果取决于是否找到值excel vba

时间:2016-05-06 10:45:03

标签: excel vba excel-vba

我有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

欢迎任何帮助。谢谢。 enter image description here

1 个答案:

答案 0 :(得分:1)

就像队友说的那样,你不需要VBA。使用&#34生成3个辅助列;在A&#34;中找到&#34;发现于B&#34; &#34;发现于C&#34;使用此公式COUNTIF(A:A;$D2)并检查是否为Y或N.请检查此图片enter image description here