简体   繁体   中英

[VBA][Excel] Find all occurrences and Sum values of nearby cell

So I have this problem:

I have sheet named Database and in there are records that I need to find in my main sheet (sheet1), then take near (to the left) cell number and sum them up all Then drop it in Database sheet right after item that I search for. (reference in screenshot) This is how my database sheet looks like:

数据库表

This needs to loop for every record in Database sheet down.

I tried to loop thru range, but I got stuck deciding how to find values without knowing range, because it's might be anywhere. You can see structure in screenshot

This is how main Sheet looks like:

在此处输入图片说明

So short description of algorithm:

It takes number from Database sheet, find all occurrences of that in Main sheet and sums all numbers together of that occurrence, then simply places it in cell next to record that we searched for in the first place

Any suggestion guys?

please forgive for my english it's not my native language. :)

You can use the following FindAll function to find the relevant values in the Main worksheet.

From there use a Range.Offset(0, 1).Value to access the values and sum them

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

It is not clear to me how you identify the values you are looking for. If they might be anywhere on the MAIN sheet, then you can use a simple SUMIF .

Assume your Range where the values "might" be would be no larger than, for example, A1:Z1000. You would just search the whole range, and return matching data in the adjacent column. So, if your search_term is in A1 :

B1: =SUMIF(MAIN!$A$1:$Z$1000,A1,MAIN!$B$1:$ZZ$1000)

If there might be confusing entries in some of the columns, you'll need to be more specific as to how you determine which columns to search.

EDIT After seeing your latest screenshots, I suggest one of the following formulas.

If the column header makes no difference, and

  • your Database sheet has the Detale code starting in A1 and extending down the column
  • In the formulas below change the 1000 to encompass all the rows that you might possibly use, even if some are blank now.
  • Note that sum_range is the same size as, but offset by one column from the criteria ranges

    B1: =SUMIF(Presu_planas!$A$40:$Z$1000,A1,Presu_planas!$B$40:$AA$1000)

If you must restrict looking for those codes only in the columns that have Detale in Row 40, then try the following:

This formula must be array-entered :

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

To array-enter a formula, after entering the formula into the cell or formula bar, hold down ctrl + shift while hitting enter . If you did this correctly, Excel will place braces {...} around the formula.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM