How to add values in a column between selected rows

RMassoudi

New Member
Joined
Jun 12, 2015
Messages
5
LineAreaProduct (Avg.)Set Marker
115x
30
2
325x
20
415x
10
5
65x

<tbody>
</tbody>

Hello all,

This is my first post here, and I was wondering if anyone could help me do the following:

I am looking to try to perform a function where the values in the "area" column are taken and averaged into the "product" column based on the location of my "set marker".

For example, Line 1 has a marker whereas Line 2 does not - therefore the area value of 30 is average between Line 1 and Line 3 (which has a marker). Keep in mind that Line 3 also shares an area value with Line 4, etc. which also has a marker.

Any ideas would be really appreciated.

Thanks,
Rod
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hello,

THere may be a formula method, but have created a macro which works for your data set.

Code:
 Sub AVERAGE()
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("A" & MY_ROWS).Value > 0 Then
            For MY_NEXT_ROWS = MY_ROWS + 1 To Range("A" & Rows.Count).End(xlUp).Row
                If Range("D" & MY_NEXT_ROWS).Value = "x" Then
                    Range("C" & MY_ROWS).Value = Range("C" & MY_ROWS).Value + Range("B" & MY_ROWS + 1).Value / 2
                    Range("C" & MY_NEXT_ROWS).Value = Range("C" & MY_NEXT_ROWS).Value + Range("B" & MY_ROWS + 1).Value / 2
                    GoTo CONT
                End If
            Next MY_NEXT_ROWS
        End If
CONT:
    Next MY_ROWS
End Sub
 
Upvote 0
Hello,

How you do you determine how the 'area' is shared out?

Between the lines denoted with an "X". If I have a value of 20 above one X and below one X - I would distribute 10 to each. If there's another value above or below either of those X'd lines, then they would be added on as well. However, if there's a value above a line with an X or below a line with an X and there's no other X to sandwich it, the full value will be transferred to that X'd line.
 
Upvote 0
Between the lines denoted with an "X". If I have a value of 20 above one X and below one X - I would distribute 10 to each. If there's another value above or below either of those X'd lines, then they would be added on as well. However, if there's a value above a line with an X or below a line with an X and there's no other X to sandwich it, the full value will be transferred to that X'd line.

Bump :)
 
Upvote 0
Hello,

Code:
Sub AVERAGE_AREA()
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("D" & MY_ROWS).Value = "x" Then
            For MY_NEXT_ROWS = MY_ROWS + 1 To Range("A" & Rows.Count).End(xlUp).Row
                If Range("B" & MY_NEXT_ROWS).Value > 0 Then
                        MY_NUMBER = Range("B" & MY_NEXT_ROWS)
                End If
                If Range("D" & MY_NEXT_ROWS).Value = "x" Then
                    Range("C" & MY_ROWS).Value = Range("C" & MY_ROWS).Value + MY_NUMBER / 2
                    Range("C" & MY_NEXT_ROWS).Value = Range("C" & MY_NEXT_ROWS).Value + MY_NUMBER / 2
                    GoTo CONT
                End If
            Next MY_NEXT_ROWS
        End If
CONT:
    Next MY_ROWS
End Sub
 
Upvote 0
Hello,

Code:
Sub AVERAGE_AREA()
    For MY_ROWS = 2 To Range("A" & Rows.Count).End(xlUp).Row
        If Range("D" & MY_ROWS).Value = "x" Then
            For MY_NEXT_ROWS = MY_ROWS + 1 To Range("A" & Rows.Count).End(xlUp).Row
                If Range("B" & MY_NEXT_ROWS).Value > 0 Then
                        MY_NUMBER = Range("B" & MY_NEXT_ROWS)
                End If
                If Range("D" & MY_NEXT_ROWS).Value = "x" Then
                    Range("C" & MY_ROWS).Value = Range("C" & MY_ROWS).Value + MY_NUMBER / 2
                    Range("C" & MY_NEXT_ROWS).Value = Range("C" & MY_NEXT_ROWS).Value + MY_NUMBER / 2
                    GoTo CONT
                End If
            Next MY_NEXT_ROWS
        End If
CONT:
    Next MY_ROWS
End Sub

This is awesome! Thank you. My apologies for looking a gift horse in the mouth, but is there anyway I can do this with formulas? I'm okay using an assortment of additional columns, etc.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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