【问题标题】:Is it possible to make a vlookup on a table and after that update the value using VBA?是否可以在表上进行 vlookup,然后使用 VBA 更新值?
【发布时间】:2017-12-29 18:50:50
【问题描述】:

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

我正在尝试这个:

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 是实现这一目标的最佳方式,但至少更容易解释我想要实现的目标。

【问题讨论】:

  • 在我们开始回答之前,要问的一个重要问题是……您是否打算将 getMaxRef 用作 UDF?如果是这样,答案是简单的“不”。但是,如果您不尝试创建 UDF,那么我们可以开始告诉您如何使用 MatchIndex 等。
  • @YowE3K 没有 getMaxRef 只会在 VBA 上使用。

标签: excel vba


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-20
    • 2011-11-21
    • 1970-01-01
    • 2015-02-11
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多