是否可以在表上进行vlookup,之后使用VBA更新值?

问题描述 投票:0回答:1

我在更新值之前将一些引用存储在我想要检索的表中。

我正在尝试这个:

Private Function getTable(sheetName As String, tableName As String) As Excel.ListObject
    Dim ws As Excel.Worksheet

    Set ws = Sheets(sheetName)

    Set getTable = ws.ListObjects(tableName)
End Function

Private Function getMaxRef(tableName As String) As Integer
    Dim lo As Excel.ListObject
    Set lo = getTable("Aux", "references")

    Dim result As Variant

    result = Application.VLookup(tableName, Range(lo), 2, False)

    getMaxRef = result
    'After this I want to change the value found by vLookup


End Function

我不认为vlookup是实现这一目标的最佳方式,但至少更容易解释我想要实现的目标。

excel-vba vba excel
1个回答
1
投票

我怀疑你想要做的是:

Private Function getMaxRef(tableName As String) As Integer
    Dim lo As Excel.ListObject
    Set lo = getTable("Aux", "references")

    Dim r As Variant

    r = Application.Match(tableName, lo.DataBodyRange.Columns(1), 0)

    If IsError(r) Then
        MsgBox "Record not found"
        getMaxRef = -1
    Else
        getMaxRef = lo.DataBodyRange.Cells(r, 2)
        'After this I want to change the value found by vLookup
        lo.DataBodyRange.Cells(r, 2) = lo.DataBodyRange.Cells(r, 2) + 1
    End If
End Function
© www.soinside.com 2019 - 2024. All rights reserved.