简体   繁体   English

在excel vba问题中查找行

[英]finding a row in excel vba issue

I have an excel spreadsheet with 3 columns. 我有3列的Excel电子表格。 Column 1, Column 2, Column 3 . Column 1, Column 2, Column 3 I want to do a find criteria so I can get back the position of the Row. 我想查找条件,以便可以重新获得行的位置。 How is this possible?. 这怎么可能?。

To find one criteria I can do ... but do not know how to extend so it finds the row that matches eg value 1, value 2 , value 3. 为了找到一个条件,我可以做...但是不知道如何扩展,因此它找到与值1,值2,值3相匹配的行。

Columns("A").Find(value, Range(A:A), xlValues, xlWhole)

You can use an array to hold the range, and then search the array to find the values. 您可以使用数组保存范围,然后搜索数组以查找值。 Once the values are found, it will output the row number which they have been found on. 找到值后,它将输出找到它们的行号。

Option Explicit
Sub matchCriteria()
    Dim arrValues() As Variant
    Dim lrow As Long, i As Long
    Dim valOne As String, valTwo As String, valThree As String

    valOne = "x" 'Update to reflect the values you want to find
    valTwo = "y"
    valThree = "z"
    lrow = Cells(Rows.Count, 1).End(xlUp).Row
    arrValues = Range("A1:C" & lrow)

    For i = 1 To UBound(arrValues, 1)
        If arrValues(i, 1) = valOne _
        And arrValues(i, 2) = valTwo _
        And arrValues(i, 3) = valThree Then
            MsgBox ("Row " & i & " contains the three specified values.")
            Exit For
        End If
    Next i
End Sub

The array currently will contain columns A:C. 该数组当前将包含列A:C。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。

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

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