Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
[VBA][Excel]查找附近单元格的所有匹配项和总和值_Vba_Excel - Fatal编程技术网

[VBA][Excel]查找附近单元格的所有匹配项和总和值

[VBA][Excel]查找附近单元格的所有匹配项和总和值,vba,excel,Vba,Excel,所以我有一个问题: 我有一个名为Database的工作表,其中有一些记录我需要在我的主工作表(sheet1)中找到,然后取附近(左侧)的单元格编号,将它们相加,然后将其放在数据库工作表中,正好在我搜索的项之后。(屏幕截图中的参考)我的数据库表是这样的: 这需要循环数据库表中的每个记录 我试图循环遍历范围,但我在决定如何在不知道范围的情况下查找值时遇到了困难,因为它可能在任何地方。您可以在屏幕截图中看到结构 这是主工作表的外观: 因此,对算法的简短描述如下: 它从数据库工作表中获取数字,在主工

所以我有一个问题:

我有一个名为Database的工作表,其中有一些记录我需要在我的主工作表(sheet1)中找到,然后取附近(左侧)的单元格编号,将它们相加,然后将其放在数据库工作表中,正好在我搜索的项之后。(屏幕截图中的参考)我的数据库表是这样的:

这需要循环数据库表中的每个记录

我试图循环遍历范围,但我在决定如何在不知道范围的情况下查找值时遇到了困难,因为它可能在任何地方。您可以在屏幕截图中看到结构

这是主工作表的外观:

因此,对算法的简短描述如下:

它从数据库工作表中获取数字,在主工作表中查找该数字的所有匹配项,并将该匹配项的所有数字相加,然后简单地将其放在我们首先搜索的记录旁边的单元格中

有什么建议吗


请原谅我的英语不是我的母语

您可以使用以下FindAll函数在主工作表中查找相关值

在此基础上,使用Range.Offset(0,1).Value访问这些值并求和

Function FindAll(What, _
    Optional SearchWhat As Variant, _
    Optional LookIn, _
    Optional LookAt, _
    Optional SearchOrder, _
    Optional SearchDirection As XlSearchDirection = xlNext, _
    Optional MatchCase As Boolean = False, _
    Optional MatchByte, _
    Optional SearchFormat) As Range

    'LookIn can be xlValues or xlFormulas, _
     LookAt can be xlWhole or xlPart, _
     SearchOrder can be xlByRows or xlByColumns, _
     SearchDirection can be xlNext, xlPrevious, _
     MatchCase, MatchByte, and SearchFormat can be True or False. _
     Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _
     object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-"""

    Dim SrcRange As Range
    If IsMissing(SearchWhat) Then
        Set SrcRange = ActiveSheet.UsedRange
    ElseIf TypeOf SearchWhat Is Range Then
        Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat)
    ElseIf TypeOf SearchWhat Is Worksheet Then
        Set SrcRange = SearchWhat.UsedRange
    Else: SrcRange = ActiveSheet.UsedRange
    End If
    If SrcRange Is Nothing Then Exit Function

    'get the first matching cell in the range first
    With SrcRange.Areas(SrcRange.Areas.Count)
        Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count)
    End With

    Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _
        SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)

    If Not CurrRange Is Nothing Then
        Set FindAll = CurrRange
        Do
            Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _
            SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
            If CurrRange Is Nothing Then Exit Do
            If Application.Intersect(FindAll, CurrRange) Is Nothing Then
                Set FindAll = Application.Union(FindAll, CurrRange)
            Else: Exit Do
            End If
        Loop
    End If
End Function

我不清楚你是如何确定你所寻找的价值观的。如果它们可能位于
MAIN
工作表的任何位置,则可以使用简单的
SUMIF

假设值“可能”所在的范围不大于,例如A1:Z1000。您只需搜索整个范围,并在相邻列中返回匹配的数据。因此,如果您的
搜索词
位于
A1

B1: =SUMIF(MAIN!$A$1:$Z$1000,A1,MAIN!$B$1:$ZZ$1000)
如果某些列中可能有令人困惑的条目,则需要更具体地确定如何确定要搜索的列

编辑在看到您最新的屏幕截图后,我建议使用以下公式之一

如果列标题没有区别,以及

  • 数据库工作表的详细代码从A1开始,向下延伸到列
  • 在下面的公式中,将1000改为包含所有可能使用的行,即使现在有些行为空
  • 请注意,
    sum\u range
    的大小与相同,但与
    标准范围偏移了一列

    B1: =SUMIF(Presu_planas!$A$40:$Z$1000,A1,Presu_planas!$B$40:$AA$1000)
    
如果必须限制仅在第40行中包含Detale的列中查找这些代码,请尝试以下操作:

此公式必须是输入的数组

B1: =SUM((Presu_planas!$A$40:$Z$40="Detale")*(Presu_planas!$A$40:$Z$1000=A1)*IFERROR(--Presu_planas!$B$40:$AA$1000,0))

数组,请在输入后输入公式 将公式输入单元格或公式栏,按住 按住ctrl+shift键的同时按enter键。如果你这么做了
正确地说,Excel将在公式周围放置大括号。

查看时如何识别范围?你可以在你的公式或宏中做同样的事情。告诉我更多。我查了SUMIF和SUMIFS的公式,但现在这些公式还不能达到我的要求。当你查看它时,你如何识别范围?通过简单地查看顶部和左侧标尺(我不知道如何称呼它们),我可以看到A1或G5等等,这是没有帮助的。“标尺”(我不知道你的意思)是什么,它让你知道你正在寻找期望的范围?不,我需要搜索的值在数据库表中,需要搜索和汇总的记录在主表中。。。这些记录可以是完整的文档,如图@cunamirok,如果你把我建议的公式放在数据库表上,它会在主表上搜索。只需根据需要调整单元格和工作表引用。只需确保您的范围包含所有内容。