代码之家  ›  专栏  ›  技术社区  ›  MsAgentM

vlookup中未分配变量

  •  0
  • MsAgentM  · 技术社区  · 6 年前

    我在表的第2列中尝试使用一个vlookup表,并从表的第8列获取数据。但是,不会分配变量。当代码到达指定的变量时,继续执行而不分配变量,然后完全跳过if语句。我没有得到错误,代码只是继续进行,好像它不在那里。有人能告诉我为什么这个变量没有从vlookup中分配数据吗?

    Option Explicit
    Dim RevSID As String
    Dim RevSupLev As String
    Dim RevActive As String
    Dim DueDate As Date
    
    
    
    Private Sub Contact_Update()
    Set CaseRng = CaseRevPvt.DataBodyRange *Another pivot table in the workbook
    Set Contact = Worksheets("Tables").ListObjects("Contact") 
    
     For Each cell In CaseRng
    
        RevSID = cell.Offset(0, 1)
        RevSupLev = cell.Offset(0, 2)
        RevActive = cell.Offset(0, 3)
    
        If RevSID = 0 Then 'An integer variable function doesn't need to run if no data
            On Error Resume Next
            End If  
    
        elseif RevActive = "No" then
             'Do stuff..works fine
        elseif RevSupLev = "String indicated" then
            if PADate>duedate then 'checks PADue for condition
                 'does stuff, this works
            else: Call StandRev 'the intent is to do a Vlookup using RevSID,
                    'find the matching data in Column2 of the Contact table and assign the
                    information in Column8 to lastrev
    

    Private Sub StandRev()
    Dim VlookUp As Range
    Dim lastrev As Date
    
    
    With Worksheets("Tables") 'sets a look up range within the table "Contact"
    Set VlookUp = Contact.Parent.Range(Contact.ListColumns("SID").DataBodyRange, Contact.ListColumns("Last Review").DataBodyRange)
    
    lastrev = Application.WorksheetFunction.VlookUp(RevSID, VlookUp, 8,False)  '*** problem here -- RevSID variable is assigned in previous sub
    ' no data is saved in variable, program ends sub
    
    If lastrev > AttempDate2 Then
        'code that will replace lastrev with data in AttempDate2, AttempDate2 varaiable assigned in another sub
    End If
    
    End With
    
    End Sub
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Mathieu Guindon    6 年前

    如果 RevSID 没有明确声明,那么 Revsid 在过程1中,变量与 Revsid 在过程2中:未声明的变量总是在本地范围内,因此在过程2中分配它不会影响过程1中同名变量的值。

    但这不是这里发生的事情。自从 Revsid 在某个地方声明,您的查找肯定失败了(即它没有找到 Revsid 查找表中的值)。

    我建议使用一种完全不同的方法,使用一个函数,和一个称为“try pattern”的模式,其中您有一个函数返回 Boolean 并输出一个通过引用传递的参数的结果,该参数只有在函数返回时才有意义的值。 True -因为一眼看上去 [SID] 列不是表中最左边的列(为什么要一直走到 Contact.Parent 否则呢?),我建议使用索引和匹配的组合来执行查找-注意列的顺序如何与此查找方法无关。

    这是一个早期版本 WorksheetFunction 调用,失败时引发运行时错误:

    Private Function TryGetRevisionDate(ByVal SID As String, ByRef outResult As Date) As Boolean
        On Error GoTo CleanFail
    
        With Application.WorksheetFunction
            Dim matchRow As Long
            matchRow = .Match(SID, Contact.ListColumns("SID").DataBodyRange, 0)
    
            Dim indexValue As Variant
            indexValue = .Index(Contact.ListColumns("Last Review").DataBodyRange, matchRow)
        End With
    
        If IsDate(indexValue) Then outResult = indexValue
        TryGetRevisionDate = True
    
    CleanExit:
        Exit Function
    
    CleanFail:
        'lookup failed
        Resume CleanExit
    End Function
    

    以及一个有延迟绑定的版本 工作表函数 呼叫,哪个 返回 失败时的错误值(请注意,您没有获得参数信息,也没有使用后期绑定代码进行编译时验证,因此请注意拼写错误- Option Explicit 无法在此处保存您):

    Private Function TryGetRevisionDate(ByVal SID As String, ByRef outResult As Date) As Boolean
        With Application
    
            Dim matchRow As Variant
            matchRow = .Match(SID, Contact.ListColumns("SID").DataBodyRange, 0)
            If IsError(matchRow) Then Exit Function
    
            Dim indexValue As Variant
            indexValue = .Index(Contact.ListColumns("Last Review").DataBodyRange, matchRow)
            If IsError(indexValue) Then Exit Function
    
        End With
    
        If IsDate(indexValue) Then
            outResult = indexValue
            TryGetRevisionDate = True
        End If
    
    End Function
    

    使用任一版本,您的调用代码现在都可以执行此操作:

    Dim revDate As Date
    If TryGetRevisionDate(RevSID, revDate) Then
        MsgBox revDate
    Else
        MsgBox "SID '" & RevSID & "' was not found."
    End If