簡體   English   中英

excel vba 搜索匹配項

[英]excel vba to search for a match

我在 Sheet1 A1 中有一個日期,而在 Sheet2 上,A 列填充了日期

我正在尋找當分配給按鈕並按下它時會向下看 Sheet2 列 A 的東西,如果任何日期匹配 Sheet1 A1,它將帶來一個消息框,說找到匹配,如果沒有找到匹配,它會彈出一個消息框說找不到匹配項

謝謝你

您可以使用此代碼。 但如果需要,不要忘記更改第二個工作表的名稱。

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

試試這個然后告訴我

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