在多个页面上的Excel上查找并返回具有关联行的值

问题描述 投票:0回答:1

在此先感谢您的帮助!我没有在excel中使用太多的VBA,也无法解决如何做我需要的事情,但我相信我需要它来实现我在工作簿中所需的功能。

我有31个数据页,我需要查找是否存在某些信息并将其显示在摘要页面上。

我需要检查列AQ中是否有值,如果有,那么我需要在E,F和G列中该行返回的数据。每张纸可能有多个实例,或者每张纸可能没有。

希望这个例子可以更好地解释它:

           E         F            G          ...        AQ
          Date      Name      Location               Exception
2       1-12-17     Dave      England
3       1-12-17    Sarah       Wales                   Exp

在上面的示例数据中,我希望在“摘要”页面上返回的信息来自第3行。(此类数据位于其他31个页面中)

希望这是有道理的!任何有关如何做到这一点的帮助将不胜感激。

excel vba excel-vba
1个回答
1
投票

有许多不同的方法可以解决这个问题,例如,具有特定过滤条件的数据透视表,找到匹配的UDF并将它们打印到您想要的输出等等。通常,这不是一个坏主意使用Range.Find方法并循环遍历所有匹配。

这需要一定的编程时间和精力,这并不是每个人都有,尽管大多数使用Excel的人最终最终都会使用vLookup。我一直对vLookup不满意,与vba Range.Find方法相比,它是如此有限。只是为了你,因为它几乎是圣诞节,我已经没有工作了,我实际上已经付了钱,这里有一个小宝石应该有助于解决你的问题。

它是一个UDF查找,允许您指定要返回的数字匹配,并返回行或列中的自定义偏移量以作为值进行检索。增加变量matchNum将为您提供范围内的所有匹配,并且您可以使用适当的偏移量返回所需的任何列。

使用Range.Find方法可以让您了解如何使用代码在不使用UDF查找功能的情况下使用您想要的完全填充工作表。我将把它作为读者的练习。

'################################################################################################################################
' findwhat: the value you want to find. (needle)
' where: the range you want to look for findwhat (haystack)
' matchNum: if the needle is found more than once in the haystack, find the nth target.
' rowoffset: offset your return value from the target, positive will return the value of the cell below the target.
' columoffset: offset your return value from the target, positive will return the value of the cell to the right of the target.
'################################################################################################################################
Public Function powerLookup(findwhat As Variant, where As Range, Optional matchNum As Long = 1, Optional rowOffset As Long = 0, Optional columnOffset As Long = 0) As Variant
    Dim rngResult As Range, firstAddress As String
    Set rngResult = Nothing
    On Error GoTo Errorhandler ' if something breaks return #NA (will definitely happen if first find call fails)
    Do While matchNum > 0 'loop through the matches until there are no matches or we've reached the target matchnumber
        'the first time, rngResult will be Nothing, so the first find can't have rngResult as an input.
        With where
            If rngResult Is Nothing Then
                Set rngResult = .find(findwhat, , xlValues)
                firstAddress = rngResult.Address 'keep this to know if we've looped past the start
            Else
                'if rngResult is not nothing we've already found a match, find the next match until matchnum is 0
                Set rngResult = .find(findwhat, rngResult, xlValues)
                If rngResult.Address = firstAddress Then
                    'if we reach the first address we've looped around, no more matches found, throw #NA error
                    powerLookup = CVErr(xlErrNA)
                    Exit Function
                End If
            End If
        End With
        matchNum = matchNum - 1
    Loop
    powerLookup = rngResult.offset(rowOffset, columnOffset).Value 'offset the output
    Exit Function
Errorhandler:
   powerLookup = CVErr(xlErrNA)
End Function
© www.soinside.com 2019 - 2024. All rights reserved.