首页 文章

需要在VBA函数中查找2列

提问于
浏览
-2

我需要使用从另一个工作表中查找列并返回单个单元格VBA . Sheet1中的我的数据是,

P.No.  REV   Qty 
2918   01    50   
2918   02    44
2919   01    72

在Sheet2中,它应该通过查看 P.No.REV 列返回 Qty . 请帮助我 .

我查找一列的代码如下 . 在这里我需要查找两列 .

Function SingleCellExtractInward(lookupvalue As String, lookuprange As Range, ColumnNumber As Integer)
    Dim i As Double
    Dim Result1 As String
    Dim Result2 As String

    If Result2 = Empty Then
        Result2 = "no recent inward"
        SingleCellExtractInward = Result2
    End If

    For i = 1 To lookuprange.Columns(1).Cells.Count
        If lookuprange.Cells(i, 1) = lookupvalue Then
            Result1 = Result1 & " " & lookuprange.Cells(i, ColumnNumber) & ","
            SingleCellExtractInward = Left(Result1, Len(Result1) - 1)
        End If
    Next i
End Function

2 回答

  • 0
    • 因此你只需要延长你的函数 lookupvalue ,所以我们有 lookupValueAlookupValueB
    Function SingleCellExtractInward(lookupValueA As String, lookupValueB As String, _
                                     lookupRange As Range, ColumnNumber As Long)
    

    请注意,我将 Integer 更改为 Long . 除非您需要与旧API(see explanation here)进行通信,否则请始终使用 Long 而不是 Integer .

    • 您需要检查是否满足两个条件:
    If lookupRange.Cells(i, 1).Value = lookupValueA And _
       lookupRange.Cells(i, 2).Value = lookupValueB Then
    

    Alternative: 如果您希望能够另外只查看一个标准A或B,如果另一个为空,则使用:

    If (lookupRange.Cells(i, 1).Value = lookupValueA Or lookupValueA = vbNullString) And _
       (lookupRange.Cells(i, 2).Value = lookupValueB Or lookupValueB = vbNullString) Then
    

    在这种情况下,您可以保留 lookupValue 之一 vbNullString (与 "" 相同),它只查找一个条件(就像您的原始函数一样) .


    以下只是改进和删除我推荐的不必要的代码:

    • i 应为 Long 而不是 Double
    Dim i As Long
    
    • If Result2 = Empty Then 此时始终为真 . 所以我们甚至不需要 Result2 .
      下列 …
    Dim Result2 As String
    
    If Result2 = Empty Then
        Result2 = "no recent inward"
        SingleCellExtractInward = Result2
    End If
    

    可以减少到一行......

    SingleCellExtractInward = "no recent inward"   'Default return value if nothing matches.
    

    所以我们最终会得到类似的东西:

    Option Explicit
    
    Function SingleCellExtractInward(lookupValueA As String, lookupValueB As String, _
                                     lookupRange As Range, ColumnNumber As Long)
        Dim i As Long
        Dim Result As String
    
        SingleCellExtractInward = "no recent inward"
    
        For i = 1 To lookupRange.Columns(1).Cells.Count
            If (lookupRange.Cells(i, 1).Value = lookupValueA Or lookupValueA = vbNullString) And _
               (lookupRange.Cells(i, 2).Value = lookupValueB Or lookupValueB = vbNullString) Then
                Result = Result & " " & lookupRange.Cells(i, ColumnNumber) & ","
                SingleCellExtractInward = Left(Result, Len(Result) - 1)
            End If
        Next i
    End Function
    
  • 0

    尝试使用此功能:

    Function LookupTwoCriteria(strPNo As String, strRev As String)
    
          LookupTwoCriteria = Evaluate("=INDEX(Sheet1!C:C,MATCH(1,(Sheet1!A:A=" & _
            strPNo & ")*(Sheet1!B:B=" & strRev & "),0))")
    
        End Function
    

    它使用数组公式来尝试匹配两个条件:strPNo和strRev . 在你的单元格中使用它,如:

    =LookupTwoCriteria(A1, B1)
    

    如果找不到匹配项,它将显示#VALUE!保护它:

    =IFERROR(LookupTwoCriteria(A4,B4),0)
    

    如果你愿意的话 .

相关问题