Sum two cells above a specific cell in different two sheets

mozaya

New Member
Joined
Dec 13, 2016
Messages
3
I have two sheets:

1: Productivity in different types for 200 labors as follow:

Date123
Labor 1Prod. 11079
Prod. 2250
Villa no.591-G340-F670-R
Labor 2Prod. 111014
Prod. 2382
Villa no.340-F591-G591-G

<tbody>
</tbody>


















2: The progress sheet for 1000 villa with three floors which i need to link with the first one that any quantity done in the same villa shall be updated directly on the second sheet:

Villa no.Executed quantity
591-G????
340-F????
670-R????

<tbody>
</tbody>

i want a formula makes me sum all the quantity don by any labor in the same villa in the first sheet to be sum and filled directly in the second sheet in the executed quantity
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Re: very complicated formula to sum two cells above a specific cell in different two sheets

welcome to the board

So if I understand this right, the correct answer in your example would be [591-G] = 10+2 + 0+8 + 14+2 = 36? Because you need to find the corresponding Prod 1 and Prod 2 entries for each instance of the Villa.

That would be a simple SUMIF, but you want to do this for 200 or even 1000 rows in each calculation, making SUMIF impractical?
 
Upvote 0
Re: very complicated formula to sum two cells above a specific cell in different two sheets

right, but what is the other solution instead of that.
that is the question.
 
Upvote 0
Re: very complicated formula to sum two cells above a specific cell in different two sheets

the fundamental problem here is badly laid-out data. If you have the option to reconstruct it so it is like a database with columns "Villa" "Labor" "date" "value" then it would be much easier. You can use VBA to quickly do that reconstruction. Without it the formula will be difficult to write

I can demonstrate a UDF approach as follows, which creates a bespoke formula for you to use in your spreadsheet:
Code:
Option Explicit
Function sumVilla(rngData As Range, rngVillaName As Range)
Dim rngVillas As Range, cl As Range, intCount As Integer

' identify all cells containing the search term
For Each cl In rngData.SpecialCells(xlCellTypeConstants, 2)
    If cl = rngVillaName Then
        If rngVillas Is Nothing Then
            Set rngVillas = cl
        Else
            Set rngVillas = Union(rngVillas, cl)
        End If
    End If
Next cl
    
' extract values from corresponding cells
If rngVillas Is Nothing Then
    sumVilla = 0
Else
    For Each cl In rngVillas
        sumVilla = sumVilla + cl.Offset(-2, 0) + cl.Offset(-1, 0)
    Next cl
End If
End Function
There are 2 problems with this code:
- its slower than a standard Excel formula, and has noticeable calculation time on just the data shown in post 1, let alone 1000 villas
- it requires macros to be enabled in order to give a result

I haven't optimised it yet and can definitely improve the speed, but I'd be looking to restructure the data if at all possible
 
Upvote 0
ADVERTISEMENT
Re: very complicated formula to sum two cells above a specific cell in different two sheets

you need to post this code in a standard VBA code module, and then write a formula in a cell, =sumvilla([your data table],[range containing villa name])
 
Upvote 0
Re: very complicated formula to sum two cells above a specific cell in different two sheets

this is a faster version of the same code. It will work better on large data sets as it uses Excel's FIND (Ctrl + F) feature to identify only those search terms of interest and ignore all other cells. It will still show a noticeable lag on calculations

Code:
Function sumVilla(rngData As Range, rngVillaName As Range)

Dim cl As Range, clFirst As Range

' identify all cells containing the search term
On Error Resume Next ' in case none exist
    Set clFirst = rngData.Find(What:=rngVillaName, After:=rngData.Cells(1, 1), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) ' find first term
On Error GoTo 0

If clFirst Is Nothing Then Exit Function
Set cl = clFirst ' identify first cell

Do ' keep finding next term until first one found again, at which point exit loop
    sumVilla = sumVilla + cl.Offset(-2, 0) + cl.Offset(-1, 0) ' add current offset values
    
    Set cl = rngData.Find(What:=rngVillaName, After:=cl, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) ' find next instance of term
    
    If cl.Address = clFirst.Address Then Exit Function ' if back at first cell then exit loop
    
Loop
End Function
 
Upvote 0
ADVERTISEMENT
Hi,

Assuming that the structure does not change ....

F4=IF(B4="Villa no.",SUMPRODUCT((C4:E4=$F$1)*(C2:E3)),0)
G4=IF(B4="Villa no.",SUMPRODUCT((C4:E4=$G$1)*(C2:E3)),0)
H4=IF(B4="Villa no.",SUMPRODUCT((C4:E4=$H$1)*(C2:E3)),0)



ABCDEFGH
1Date10/12/201610/12/201610/12/2016340-F591-G670-R
2Labor 1Prod. 11079
3Prod. 2250
4Villa no.591-G340-F670-R12129
5Labor 2Prod. 111014000
6Prod. 2382000
7Villa no.340-F591-G591-G14240
8Labor 3Prod. 111014000
9Prod. 2382000
10Villa no.340-F591-G591-G14240
11Labor 4Prod. 134014000
12Prod. 2382000
13Villa no.340-F670-R591-G37168
14Labor 5Prod. 111014000
15Prod. 2682000
16Villa no.340-F591-G591-G17240
Total9410017

<tbody>
</tbody>
 
Last edited:
Upvote 0
=IF(Sheet1!B4="villa no.",SUMPRODUCT((Sheet1!C4:E4=$H$3)*(Sheet1!E2:E3)),0)

i want to generate this formula horizontally, so every thing is ok except (Sheet1!E2:E3 can't be generated.
can you send me how to generate it to be (Sheet1!f2:f3),(Sheet1!g2:g3),(Sheet1!h2:h3)
 
Upvote 0

Forum statistics

Threads
1,196,028
Messages
6,012,956
Members
441,740
Latest member
IammeResources

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top