excel vba搜索匹配

问题描述 投票:1回答:2

我在Sheet1 A1和Sheet2列A上有一个日期

即时寻找分配给按钮并按下它的东西将向下看Sheet2列A,如果任何日期与Sheet1 A1匹配,它将带一个消息框说匹配找到,如果没有找到匹配,它将弹出一个消息框说找不到匹配

谢谢

excel vba
2个回答
1
投票

您可以使用此代码。但是如果需要,请不要忘记更改第二个工作表的名称。

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

0
投票

试试这个并告诉我

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
© www.soinside.com 2019 - 2024. All rights reserved.