未在vlookup中分配变量

时间:2019-02-07 15:22:56

标签: excel vba

我有一个表试图在表的column2中进行vlookup并从表的column8中获取数据。但是,不会分配该变量。当代码到达指示的变量时,请在不分配变量的情况下继续进行操作,然后完全跳过if语句。我没有收到错误,代码只是继续进行,好像它不在那儿一样。有人可以告诉我为什么未从vlookup分配此变量

class MySidekiqTask
  include Sidekiq::Worker

  def perform
    application_name = Rails.application.class.parent_name
    application = Object.const_get(application_name)
    application::Application.load_tasks
    Rake::Task['db:migrate'].invoke
  end
end

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

1 个答案:

答案 0 :(得分:1)

如果未显式声明RevSID,则procedure1中的RevSID与process2中的RevSID不会是同一变量:未声明的变量始终是局部范围的,因此将其分配给procedure2不会影响procedure1中同名变量的值。

但这不是这里发生的事情。由于RevSID 是在某处声明的,因此您的查找必须失败(即,在查找表中找不到RevSID的值)。

我将建议一种截然不同的方法,改用一个函数,以及一个称为“ try pattern”的模式,在该模式中,您可以使用一个函数返回一个Boolean并在传递的参数中输出结果通过引用,该函数仅在函数返回True时才具有有意义的值-而且一目了然,看来[SID]列不是表中最左侧的(您为什么要去一直到Contact.Parent都这样吗?),我建议结合使用INDEX和MATCH来执行查找-请注意,这种查找方法与列的顺序无关。

这是一个带有早期绑定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

以及具有后期绑定WorksheetFunction调用的版本,该版本会在失败时返回一个错误值(请注意,您不会获得任何参数信息,也不会使用后期绑定进行编译时验证代码,请注意拼写错误-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
相关问题