简体   繁体   English

如何匹配/搜索excel VBA中Sheet2.Range(“A1:A10”)中的Sheet1.Range(“A1”)的值

[英]How to match/search the value of Sheet1.Range(“A1”) in Sheet2.Range(“A1:A10”) in excel VBA

Let's say i have this value "ABC123" in Sheet1.Range("A1") 假设我在Sheet1.Range("A1")有这个值“ABC123”

I want to search for/ match this value in Sheet2.Range("A1:A10") // or the column 我想在Sheet2.Range(“A1:A10”)//或列中搜索/匹配此值

If the value is found
      //msgbox "Found"
else
     //msgbox "Not found"
end if

try this: 尝试这个:

Sub foo()
    Dim t As Long
        On Error Resume Next
        t = Application.WorksheetFunction.Match(Worksheets("Sheet1").Range("A1"), Worksheets("Sheet2").Range("A:A"), 0)
        On Error GoTo 0
        If t > 0 Then
            MsgBox "Found"
        Else
            MsgBox "Not found"
        End If
End Sub

Try using the Match function below (you will get the row number found): 尝试使用下面的Match功能(您将获得行号):

Option Explicit

Sub MatchTest()

Dim MatchRes As Variant

MatchRes = Application.Match(Worksheets("Sheet1").Range("A1").Value, Worksheets("Sheet2").Range("A1:A10"), 0)
If IsError(MatchRes) Then
    MsgBox "Not found"
Else
    MsgBox "Found at row " & MatchRes
End If

End Sub

Consider: 考虑:

Sub dural()
    Dim s As String, r1 As Range, r2 As Range, r3 As Range

    Set r1 = Sheet1.Range("A1")
    Set r2 = Sheet2.Range("A1:A10")
    s = r1.Value

    Set r3 = r2.Find(what:=s, after:=r2(1))
    If r3 Is Nothing Then
        MsgBox "not found"
    Else
        MsgBox "Found"
    End If
End Sub

just playing around a little bit with Scott's solution: 只是玩一下斯科特的解决方案:

Sub foo2()
    Dim t As Long
    On Error GoTo NotFound
    t = Application.WorksheetFunction.Match(Worksheets("Sheet1").Range("A1"), Worksheets("Sheet2").Range("A:A"), 0)
    MsgBox "Found
    Exit Sub      
NotFound:
    MsgBox "Not found"
End Sub

暂无
暂无

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

相关问题 有没有一种简洁的方法来做 Range(“A1:A10”).Value = Range(“A1:A10”).Value + 1 in vba - Is there a concise way to do Range(“A1:A10”).Value = Range(“A1:A10”).Value + 1 in vba 如果 LEN(Sheet1.Range("A1") > 0) 返回 0,但仍在运行代码? - If LEN(Sheet1.Range("A1") > 0) Returning 0, But Still Running Code? Excel VBA paramArray与-A1:A10不太相似 - Excel VBA paramArray doesn't much like -A1:A10 使用ThisWorkbook.Sheets(1)的Excel vba代码.Range不工作,但Sheet1.Range工作正常。 为什么? - Excel vba code using ThisWorkbook.Sheets(1).Range not working, but Sheet1.Range works fine. Why? Sheet2.Range字符串搜索的类型不匹配错误 - Type Mismatch Error for Sheet2.Range String Search Excel VBA:将每个工作表名称更改为每个工作表中的单元格 A1 值 - Excel VBA: Change each sheet name to cell A1 value in each sheet Sheets(“sheet_1”).ActiveCell.Offset(0, i + 1).Range(“A1”).Value 的正确语法是什么 - What is the correct syntax for Sheets(“sheet_1”).ActiveCell.Offset(0, i + 1).Range(“A1”).Value 如何在A1到A10提示输入框之间创建单击功能,以便用户在vba中输入数字 - How can i create click function between A1 to A10 prompt inputbox for user to enter numbers in vba range.copy目标“A1”与“Cells(1,1)”Excel / VBA - range.copy Destination “A1” vs “Cells(1,1)” Excel/VBA 使用Excel中的VBA选择从A1到文件末尾的范围 - Select range from A1 to end of file using VBA in Excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM