简体   繁体   English

excel vba 搜索匹配项

[英]excel vba to search for a match

i have a date in Sheet1 A1 and on Sheet2 column A is filled with dates我在 Sheet1 A1 中有一个日期,而在 Sheet2 上,A 列填充了日期

im looking for something that when assigned to a button and pressed it will look down Sheet2 column A and if any of the dates match Sheet1 A1 it will bring a message box saying match found and if no match is found it will bring up a message box saying no match found我正在寻找当分配给按钮并按下它时会向下看 Sheet2 列 A 的东西,如果任何日期匹配 Sheet1 A1,它将带来一个消息框,说找到匹配,如果没有找到匹配,它会弹出一个消息框说找不到匹配项

thank you谢谢你

You can use this code.您可以使用此代码。 But don't forget to change the name of the second worksheet if it is required.但如果需要,不要忘记更改第二个工作表的名称。

Private Sub CommandButton1_Click()

    Dim cell As Variant
    Dim rangeSheet As Worksheet
    Set rangeSheet = Worksheets("Sheet2")
    Dim checker As Boolean
        checker = False

    Dim lastRow As Variant
        lastRow = rangeSheet.Cells(Rows.Count, "A").End(xlUp).Row

    For Each cell In rangeSheet.Range("A1:A" & lastRow)
        If cell.Value = ActiveCell.Value Then
            checker = True
            Exit For
        End If
    Next cell

    If checker = True Then
        MsgBox ("Found")
    Else
        MsgBox ("Not found")
    End If

End Sub

Try this and tell me试试这个然后告诉我

Option Explicit

Private Sub CommandButton1_Click()

    Dim Sheet1 As Worksheet, Sheet2 As Worksheet
    Dim TheDate As String
    Dim c As Range
    Dim Last As Integer

    'Put your own name for your Worksheets
    Set Sheet1 = Worksheets("Feuil1")
    Set Sheet2 = Worksheets("Feuil2")

    'I put the value that you are looking for in the first Worksheets in cells B1
    TheDate = Sheet1.Range("B1").Value

    Last = Sheet2.Range("a65000").End(xlUp).Row

    'All your date are only in the columns A
    Set c = Sheet2.Range("A1:A" & Last).Find(TheDate, LookIn:=xlValues)


    If Not c Is Nothing Then
        MsgBox ("Found ! : " & c.Address)
        Set c = Nothing
    Else
        MsgBox "No match for : " & TheDate
    End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM