Average of Named Range

jjlafond

Board Regular
Joined
Jul 17, 2014
Messages
56
Hello,

I have a long string of numbers, some negative some positive (representing a Sine curve).

I am trying to average consecutive set of positive and negative numbers (average the 10 positive numbers, then 9 negative numbers, then the following 11 positive numbers, etc)

I currently have:
Code:
'All values have been reduced to 1, 0, -1 using Sgn()
Set SinRange = Range("E3")For r = 3 To Range("A" & Rows.Count).End(xlUp).Row
    g = r - 1
    h = r + 1
    If Cells(r, "F").Value = Cells(g, "F").Value Then
        SinRange = Union(SinRange, Cells(r, "E"))
    ElseIf Cells(r, "F").Value = "0" Then
        SinRange = Union(SinRange, Cells(r, "E"))
    Else:
        Cells(r, "G").Value = WorksheetFunction.Average(SinRange.Value)
        Set SinRange = Cells(h, "E")
    End If
Next

I've tried several different variations to no effect. I believe my issue is in the average.

I am currently trying to add a cell to the named range SinRange if it has the same sign, then taking an average of all of those grouped cells. When it "averages", it only has one number and it takes the "average" of that one number.

With this current method, I need to have all values in SinRange to be individually represented so I can take an average of the 10 or so positive numbers.

Any ideas or suggestions?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,

I have not checked the full logic but I can see where something is missing.

This:

Code:
SinRange = Union(SinRange, Cells(r, "E"))
should be;
Code:
Set SinRange = Union(SinRange, Cells(r, "E"))
 
Upvote 0

Forum statistics

Threads
1,223,648
Messages
6,173,566
Members
452,520
Latest member
Pingaware

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