简体   繁体   English

VBA查找范围内的单元格值

[英]VBA Finding cell value in a range

I am doing an algorithm and wanted to do a try-out in Excel VBA. 我正在做一个算法,想在Excel VBA中进行试用。 After some criteria is met, I want to find a cell value in a certain range (and it is definitively there) but VBA doesn't seem to find it. 在满足一些标准后,我想找到一定范围内的单元格值(并且它确定存在),但VBA似乎没有找到它。

I have looked for similar cases and also have tried diming variables and adding them to my code, but it doesn't seem to work. 我已经查找了类似的情况,并尝试了diming变量并将它们添加到我的代码中,但它似乎没有用。 When I try to use debug.print for the Range("AA2:AA16"), it runs into a error. 当我尝试使用debug.print作为Range(“AA2:AA16”)时,它会遇到错误。 It works for the other range. 它适用于其他范围。

Sheets(9).Range("AA2:AA16").Find (Sheets(9).Range("AK2").Value)     
Debug.Print Sheets(9).Range("AA2:AA16").Find(Sheets(9).Range("AK2").Value).Adress

Sheets(9).Range("AA1:AD1").Find (Sheets(9).Range("AM3").Value)
Debug.Print Sheets(9).Range("AA1:AD1").Find(Sheets(9).Range("AM3").Value).Address

I expect it to find the AK2 value in the "AA2:AA16" range and return the address. 我希望它能在“AA2:AA16”范围内找到AK2值并返回地址。 After that I want the code to select the line of that find, to select the column of the AM3 value in range "AA1:AD1" and then intersect both and sum 1 to that intersection. 之后,我希望代码选择该查找的行,在范围“AA1:AD1”中选择AM3值的列,然后将两者和和1相交到该交集。

I hope I got your problem right. 我希望我的问题正确。 Try to understand this code, it should guide you. 试着理解这段代码,它应该引导你。

示例截图

Sub test()
    Dim r1 As Range, r2 As Range, r3 As Range

    Set r1 = Sheets(1).Range("A2:A8").Find(Sheets(1).Range("A13"))
    If Not r1 Is Nothing Then
        Debug.Print r1.Address
    Else
        Debug.Print "not found"
    End If

    Set r2 = Sheets(1).Range("B1:D1").Find(Sheets(1).Range("A14"))
    If Not r2 Is Nothing Then
        Debug.Print r2.Address
    Else
        Debug.Print "not found"
    End If

    If Not (r1 Is Nothing Or r2 Is Nothing) Then
        Set r3 = Cells(r1.Row, r2.Column)
        Debug.Print r3.Address
        Debug.Print r3.Value
    Else
        Debug.Print "not found"
    End If
End Sub

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

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