0

我有一个名为“Master_Table”的表,其中包含“资产编号”列。

我想遍历该列以查看每个值是否在另一个表中('表'每日'列'资产编号'),然后更新'Master_Table'上的'Found'列。

我设法遍历一列并检查另一个表。如何根据结果更新列?

Sub updateMasterColumns() 'strCol As String)

    Dim rng As Range
    Dim cell As Range
    Dim i As Long

    Set rng = Worksheets("Master Sheet").Range("Master_Table[Asset Number]")
    
    For Each cell In rng
    
        If Not IsError(Application.Match(cell, Worksheets("Daily").ListObjects("DailyDump").ListColumns("Asset Number").Range, 0)) Then
            Debug.Print "Value exists"
        Else
            Debug.Print "Value does not exist"
        End If

    Next

End Sub

我需要它是特定于表格列的,因为表格每天都在增长。

4

1 回答 1

1

由于您使用的是表(列表对象),因此我复制了我构建的这个函数来查找表值。也许这对你来说太过分了,但它运作良好。

请记住结果可以=“”。

Function getValueFromTable(theTable As ListObject, lookupValue As String, referenceCol As String, retrieveCol As String) As String
'index/match to retrieve a value from a table, given another value
    
    With Application.WorksheetFunction
    
        Dim Result As String
        
        On Error Resume Next 'ensures result returns no string when match not found
        Result = .index(theTable.ListColumns(retrieveCol).DataBodyRange, .Match(lookupValue, theTable.ListColumns(referenceCol).DataBodyRange, 0))
        
        'because some results are stored as numbers so string value will not find
        If Result = "" And IsNumeric(lookupValue) Then
            Result = .index(theTable.ListColumns(retrieveCol).DataBodyRange, .Match(CLng(lookupValue), theTable.ListColumns(referenceCol).DataBodyRange, 0))
        End If
        On Error GoTo 0
        
    End With
    
    getValueFromTable = Result

End Function
于 2020-07-17T13:52:12.420 回答