列中的vba检查值然后检查另一列中的日期和年份?

时间:2014-10-28 18:12:52

标签: excel vba

我正在工作表2的A栏中搜索一个值。下面的代码工作正常,如果找到该值,则会显示找到的消息,如果找不到该值,则显示未找到该消息。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$1" Then
    Dim FindString As String
    Dim Rng As Range
    FindString = Range("B1").Value
    If Trim(FindString) <> "" Then
        With Sheets("Sheet2").Range("A:A") 'searches all of column A
            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
                MsgBox "found" 'value not found
            Else
                MsgBox "Nothing found" 'value not found
            End If
        End With
    End If
    End If
    End Sub

我想要做的是添加到此代码,以便在列A中搜索一个值,还在列B中搜索不同的值?然后,如果两个值都存在,则说发现别人说没找到。

请有人能告诉我如何做到这一点? 感谢

1 个答案:

答案 0 :(得分:0)

为了与您的流程保持一致,也许可以添加另一个查找:

With Sheets("Sheet2").Range("A:A") 'searches all of column A
            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
                MsgBox "col A value found"
                FindString = "<different find string>"
                With Sheets("Sheet2").Range("B:B") 'searches all of column B
                Set Rng = .Find(What:="<different find string>", _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                    If Not Rng Is Nothing Then
                        MsgBox "both values found" 'value found
                    Else
                        MsgBox "col B values not found" 'value not found
                    End If
            Else
                MsgBox "col A value not found" 'value not found
            End If
        End With