Find a row from Excel table using VBA

后端 未结 1 1927
悲&欢浪女
悲&欢浪女 2021-01-06 10:35

Within Excel, I use tables to store dynamic data inside a seperate worksheet. Entering new data works like a charm, however, I would like to be able to dynamically retrieve

相关标签:
1条回答
  • 2021-01-06 11:39

    This will return a reference the row that matches Key in a specified table

    Function GetRow(TableName As String, ColumnNum As Long, Key As Variant) As Range
        On Error Resume Next
        Set GetRow = Range(TableName) _
            .Rows(WorksheetFunction.Match(Key, Range(TableName).Columns(ColumnNum), 0))
        If Err.Number <> 0 Then
            Err.Clear
            Set GetRow = Nothing
        End If
    End Function
    

    Example use

    Sub zx()
        Dim r As Range
        Set r = GetRow("MyTable", 1, 2)
        If Not r Is Nothing Then
            r.Select
        End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题