简体   繁体   English

Excel VBA - 在表列中查找给定值的行号

[英]Excel VBA - Find Row Number for Given Value in a Table Column

Background背景

I built this code a while ago and it works in one spreadsheet.我不久前构建了这段代码,它可以在一个电子表格中运行。 It essentially gets called to find the table row number for a given value in the 1st table column.它实际上被调用以在第一个表列中查找给定值的表行号。 The rest of the code then uses this table row number to update the values for that row.代码 rest 然后使用此表行号更新该行的值。

I recently applied the same to another spreadsheet and it was working until yesterday.我最近将相同的应用到另一个电子表格,它一直工作到昨天。 Now on the line myArray = tbl.DataBodyRange I get a Run-time error '6' (Overflow).现在在 myArray = tbl.DataBodyRange 行上,我收到运行时错误“6”(溢出)。 The table in the recent spreadsheet has much more data, so myArray can no longer hold the table data.最近的电子表格中的表有更多的数据,所以 myArray 不能再保存表数据。

I have revised my code to search through the table rows using ListRows and then checking each value of the 1st column until I find what I am looking for.我修改了我的代码以使用 ListRows 搜索表格行,然后检查第一列的每个值,直到找到我要查找的内容。

In both routines, if the value is not found, it returns 0 and the other code knows not to attempt to update the table row.在这两个例程中,如果未找到该值,则返回 0 并且其他代码知道不要尝试更新表行。

Question

Am I likely to come across further issues with my revised approach and/or is there a more efficient way to find the row number I'm looking for.我是否可能会在修改后的方法中遇到更多问题和/或是否有更有效的方法来查找我正在寻找的行号。 The table currently has about 700 rows of data and will grow to over 4,000 over the next few months.该表目前有大约 700 行数据,并将在接下来的几个月内增长到 4,000 多行。

Code with Overflow Error溢出错误的代码

Function getRowNum(ByVal valueToFind As String)
    Dim tbl As ListObject
    Dim myArray As Variant
    Dim x As Long
    Dim checkvalueToFind As String
    Dim rowFound As Integer
    
    rowFound = 0
    
    Set tbl = Range("table_masterList").ListObject
    
    myArray = tbl.DataBodyRange
    
    For x = LBound(myArray) To UBound(myArray)
        checkvalueToFind = myArray(x, 1)
        'Debug.Print checkvalueToFind
        If checkvalueToFind = valueToFind Then
            rowFound = x
            GoTo foundIt
        End If
    Next x
    
foundIt:
    
    Set tbl = Nothing
    getRowNum = rowFound
    
End Function

Revised Code修改后的代码

Function getRowNum2(ByVal valueToFind As String)
    Dim tbl As ListObject
    Dim row As ListRow
    Dim checkvalueToFind As String
    Dim rowFound As Integer
    
    rowFound = 0
    
    Set tbl = Range("table_masterList").ListObject
    
    For Each row In tbl.ListRows
        checkvalueToFind = tbl.DataBodyRange.Cells(row.Index, 1).Value
         If checkvalueToFind = valueToFind Then
            rowFound = row.Index
            GoTo foundIt
        End If
    Next row
        
foundIt:
    
    Set tbl = Nothing
    getRowNum2 = rowFound
    
End Function

Looping is over-complicating.循环过于复杂。 Just use Match :只需使用Match

Function getRowNum(ByVal valueToFind As String) As Long
    ...
    Dim matchResult As Variant
    matchResult = Application.Match(valueToFind, tbl.ListColumns(1).DataBodyRange, 0)

    If IsError(matchResult) Then
       getRowNum = 0
    Else
       getRowNum = matchResult
    End If
End Function

Or slightly simpler:或者稍微简单一点:

Function getRowNum(ByVal valueToFind As String) As Long
    ...
    Dim matchResult As Variant
    matchResult = Application.Match(valueToFind, tbl.ListColumns(1).DataBodyRange, 0)

    If Not IsError(matchResult) Then
       getRowNum = matchResult
    End If
End Function

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

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