PC Review


Reply
Thread Tools Rate Thread

How to calculate Min,Median,Max values in non consecutive rows via

 
 
Pete
Guest
Posts: n/a
 
      8th Apr 2009
Hi,

I would like to calculate the Min, Median and Max values in non consecutive
rows via VBA.

Year Value
2007 10
2007 20
2008 11
2009 12
2007 30
2008 25
2009 55
2008 26
2008 16
2009 40

I would like to represent the results as follows:

2007 Min = xxx
Median = xxx
Max = xxx

2008 Min = xxx
Median = xxx
Max = xxx

2009 Min = xxx
Median = xxx
Max = xxx

Any idea what my code would look like?

Pete.
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      8th Apr 2009
You can calculate your statistics (even non-consecutive) without any VBA:

=MAX(IF(A1:A10=2007,B1:B10,""))
=MIN(IF(A1:A10=2007,B1:B10,""))
=MEDIAN(IF(A1:A10=2007,B1:B10,""))

and will display:

30
10
20


These are array formulae. They must be entered with CNTRL-SHFT-ENTER rather
than just the ENTER key.

If you must use VBA, then use it to insert the equations!
--
Gary''s Student - gsnu200844


"Pete" wrote:

> Hi,
>
> I would like to calculate the Min, Median and Max values in non consecutive
> rows via VBA.
>
> Year Value
> 2007 10
> 2007 20
> 2008 11
> 2009 12
> 2007 30
> 2008 25
> 2009 55
> 2008 26
> 2008 16
> 2009 40
>
> I would like to represent the results as follows:
>
> 2007 Min = xxx
> Median = xxx
> Max = xxx
>
> 2008 Min = xxx
> Median = xxx
> Max = xxx
>
> 2009 Min = xxx
> Median = xxx
> Max = xxx
>
> Any idea what my code would look like?
>
> Pete.

 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      8th Apr 2009
Thanks Gary's Student.

I might have over simplified my example. My IF condiction is a little more
involved than just a year. Here is what I currently have.

******************
'Check each Row in Target range for dates
' between FY start and FY end dates
For RowOffset = 1 To Target.Rows.Count
'check if Submission date in Row is not blank and,
'check if Submission date in Row is greater or equal to FY start date and,
'check if Submission date in Row is less or equal to FY end date and,
'check if variance in row is not blank.
If (Target(RowOffset, 5) <> "" And _
Target(RowOffset, 5) >= StartDate And _
Target(RowOffset, 5) <= EndDate And _
Target(RowOffset, 22) <> "") Then

'if the dates are in range then increase DrugCount and add the
variance value
DrugCount = DrugCount + 1
DrugAverage = DrugAverage + Target(RowOffset, 22)

'if the variance is 0 or less then increase the AdhereCount
If Target(RowOffset, 22) <= 0 Then
AdhereCount = AdhereCount + 1

End If

End If
Next RowOffset

'Calculate the average
If DrugCount = 0 Then
DrugAverage = 0
Else: DrugAverage = DrugAverage / DrugCount

End If

'Calculate the Adherence %
If AdhereCount = 0 Then
AdherePct = 0
Else: AdherePct = (AdhereCount / DrugCount) * 100

End If


DrugVarianceSub = DrugAverage & " " & "days" & " " & "(" & AdherePct & "%)"

**************************

However, the 'Average' value is not what the boss wants. They want Min,
Median and Max values.

How can I incorporate that in the above code?




"Gary''s Student" wrote:

> You can calculate your statistics (even non-consecutive) without any VBA:
>
> =MAX(IF(A1:A10=2007,B1:B10,""))
> =MIN(IF(A1:A10=2007,B1:B10,""))
> =MEDIAN(IF(A1:A10=2007,B1:B10,""))
>
> and will display:
>
> 30
> 10
> 20
>
>
> These are array formulae. They must be entered with CNTRL-SHFT-ENTER rather
> than just the ENTER key.
>
> If you must use VBA, then use it to insert the equations!
> --
> Gary''s Student - gsnu200844
>
>
> "Pete" wrote:
>
> > Hi,
> >
> > I would like to calculate the Min, Median and Max values in non consecutive
> > rows via VBA.
> >
> > Year Value
> > 2007 10
> > 2007 20
> > 2008 11
> > 2009 12
> > 2007 30
> > 2008 25
> > 2009 55
> > 2008 26
> > 2008 16
> > 2009 40
> >
> > I would like to represent the results as follows:
> >
> > 2007 Min = xxx
> > Median = xxx
> > Max = xxx
> >
> > 2008 Min = xxx
> > Median = xxx
> > Max = xxx
> >
> > 2009 Min = xxx
> > Median = xxx
> > Max = xxx
> >
> > Any idea what my code would look like?
> >
> > Pete.

 
Reply With Quote
 
Jerry W. Lewis
Guest
Posts: n/a
 
      9th Apr 2009
You still don't need VBA; just expand on the conditions in the array formulas

=MAX(IF((A1:A10>=DATEVALUE("11/15/2006))*(A1:A10<=DATEVALUE("4/15/2007)),B1:B10))
=MIN(IF((A1:A10>=DATEVALUE("11/15/2006))*(A1:A10<=DATEVALUE("4/15/2007)),B1:B10))
=MEDIAN(IF((A1:A10>=DATEVALUE("11/15/2006))*(A1:A10<=DATEVALUE("4/15/2007)),B1:B10))

will calculate the corresponding statistics for all dates within the
specified range, and will execute much faster than the corresponding VBA.

Note that the ,"" alternative in original IF functions are not necessary
since they would naturally be a BOOLEAN value which the statatistics
functions would ignore anyway.

Jerry

"Pete" wrote:

> Thanks Gary's Student.
>
> I might have over simplified my example. My IF condiction is a little more
> involved than just a year. Here is what I currently have.
>
> ******************
> 'Check each Row in Target range for dates
> ' between FY start and FY end dates
> For RowOffset = 1 To Target.Rows.Count
> 'check if Submission date in Row is not blank and,
> 'check if Submission date in Row is greater or equal to FY start date and,
> 'check if Submission date in Row is less or equal to FY end date and,
> 'check if variance in row is not blank.
> If (Target(RowOffset, 5) <> "" And _
> Target(RowOffset, 5) >= StartDate And _
> Target(RowOffset, 5) <= EndDate And _
> Target(RowOffset, 22) <> "") Then
>
> 'if the dates are in range then increase DrugCount and add the
> variance value
> DrugCount = DrugCount + 1
> DrugAverage = DrugAverage + Target(RowOffset, 22)
>
> 'if the variance is 0 or less then increase the AdhereCount
> If Target(RowOffset, 22) <= 0 Then
> AdhereCount = AdhereCount + 1
>
> End If
>
> End If
> Next RowOffset
>
> 'Calculate the average
> If DrugCount = 0 Then
> DrugAverage = 0
> Else: DrugAverage = DrugAverage / DrugCount
>
> End If
>
> 'Calculate the Adherence %
> If AdhereCount = 0 Then
> AdherePct = 0
> Else: AdherePct = (AdhereCount / DrugCount) * 100
>
> End If
>
>
> DrugVarianceSub = DrugAverage & " " & "days" & " " & "(" & AdherePct & "%)"
>
> **************************
>
> However, the 'Average' value is not what the boss wants. They want Min,
> Median and Max values.
>
> How can I incorporate that in the above code?
>
>
>
>
> "Gary''s Student" wrote:
>
> > You can calculate your statistics (even non-consecutive) without any VBA:
> >
> > =MAX(IF(A1:A10=2007,B1:B10,""))
> > =MIN(IF(A1:A10=2007,B1:B10,""))
> > =MEDIAN(IF(A1:A10=2007,B1:B10,""))
> >
> > and will display:
> >
> > 30
> > 10
> > 20
> >
> >
> > These are array formulae. They must be entered with CNTRL-SHFT-ENTER rather
> > than just the ENTER key.
> >
> > If you must use VBA, then use it to insert the equations!
> > --
> > Gary''s Student - gsnu200844
> >
> >
> > "Pete" wrote:
> >
> > > Hi,
> > >
> > > I would like to calculate the Min, Median and Max values in non consecutive
> > > rows via VBA.
> > >
> > > Year Value
> > > 2007 10
> > > 2007 20
> > > 2008 11
> > > 2009 12
> > > 2007 30
> > > 2008 25
> > > 2009 55
> > > 2008 26
> > > 2008 16
> > > 2009 40
> > >
> > > I would like to represent the results as follows:
> > >
> > > 2007 Min = xxx
> > > Median = xxx
> > > Max = xxx
> > >
> > > 2008 Min = xxx
> > > Median = xxx
> > > Max = xxx
> > >
> > > 2009 Min = xxx
> > > Median = xxx
> > > Max = xxx
> > >
> > > Any idea what my code would look like?
> > >
> > > Pete.

 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      9th Apr 2009
Thanks Jerry, I tried the expanded conditions and it worked.

Thank you both for our speedy replies.

Pete.

"Jerry W. Lewis" wrote:

> You still don't need VBA; just expand on the conditions in the array formulas
>
> =MAX(IF((A1:A10>=DATEVALUE("11/15/2006))*(A1:A10<=DATEVALUE("4/15/2007)),B1:B10))
> =MIN(IF((A1:A10>=DATEVALUE("11/15/2006))*(A1:A10<=DATEVALUE("4/15/2007)),B1:B10))
> =MEDIAN(IF((A1:A10>=DATEVALUE("11/15/2006))*(A1:A10<=DATEVALUE("4/15/2007)),B1:B10))
>
> will calculate the corresponding statistics for all dates within the
> specified range, and will execute much faster than the corresponding VBA.
>
> Note that the ,"" alternative in original IF functions are not necessary
> since they would naturally be a BOOLEAN value which the statatistics
> functions would ignore anyway.
>
> Jerry
>
> "Pete" wrote:
>
> > Thanks Gary's Student.
> >
> > I might have over simplified my example. My IF condiction is a little more
> > involved than just a year. Here is what I currently have.
> >
> > ******************
> > 'Check each Row in Target range for dates
> > ' between FY start and FY end dates
> > For RowOffset = 1 To Target.Rows.Count
> > 'check if Submission date in Row is not blank and,
> > 'check if Submission date in Row is greater or equal to FY start date and,
> > 'check if Submission date in Row is less or equal to FY end date and,
> > 'check if variance in row is not blank.
> > If (Target(RowOffset, 5) <> "" And _
> > Target(RowOffset, 5) >= StartDate And _
> > Target(RowOffset, 5) <= EndDate And _
> > Target(RowOffset, 22) <> "") Then
> >
> > 'if the dates are in range then increase DrugCount and add the
> > variance value
> > DrugCount = DrugCount + 1
> > DrugAverage = DrugAverage + Target(RowOffset, 22)
> >
> > 'if the variance is 0 or less then increase the AdhereCount
> > If Target(RowOffset, 22) <= 0 Then
> > AdhereCount = AdhereCount + 1
> >
> > End If
> >
> > End If
> > Next RowOffset
> >
> > 'Calculate the average
> > If DrugCount = 0 Then
> > DrugAverage = 0
> > Else: DrugAverage = DrugAverage / DrugCount
> >
> > End If
> >
> > 'Calculate the Adherence %
> > If AdhereCount = 0 Then
> > AdherePct = 0
> > Else: AdherePct = (AdhereCount / DrugCount) * 100
> >
> > End If
> >
> >
> > DrugVarianceSub = DrugAverage & " " & "days" & " " & "(" & AdherePct & "%)"
> >
> > **************************
> >
> > However, the 'Average' value is not what the boss wants. They want Min,
> > Median and Max values.
> >
> > How can I incorporate that in the above code?
> >
> >
> >
> >
> > "Gary''s Student" wrote:
> >
> > > You can calculate your statistics (even non-consecutive) without any VBA:
> > >
> > > =MAX(IF(A1:A10=2007,B1:B10,""))
> > > =MIN(IF(A1:A10=2007,B1:B10,""))
> > > =MEDIAN(IF(A1:A10=2007,B1:B10,""))
> > >
> > > and will display:
> > >
> > > 30
> > > 10
> > > 20
> > >
> > >
> > > These are array formulae. They must be entered with CNTRL-SHFT-ENTER rather
> > > than just the ENTER key.
> > >
> > > If you must use VBA, then use it to insert the equations!
> > > --
> > > Gary''s Student - gsnu200844
> > >
> > >
> > > "Pete" wrote:
> > >
> > > > Hi,
> > > >
> > > > I would like to calculate the Min, Median and Max values in non consecutive
> > > > rows via VBA.
> > > >
> > > > Year Value
> > > > 2007 10
> > > > 2007 20
> > > > 2008 11
> > > > 2009 12
> > > > 2007 30
> > > > 2008 25
> > > > 2009 55
> > > > 2008 26
> > > > 2008 16
> > > > 2009 40
> > > >
> > > > I would like to represent the results as follows:
> > > >
> > > > 2007 Min = xxx
> > > > Median = xxx
> > > > Max = xxx
> > > >
> > > > 2008 Min = xxx
> > > > Median = xxx
> > > > Max = xxx
> > > >
> > > > 2009 Min = xxx
> > > > Median = xxx
> > > > Max = xxx
> > > >
> > > > Any idea what my code would look like?
> > > >
> > > > Pete.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transposing data from consecutive rows into non-consecutive rows econ Microsoft Excel Misc 0 10th Mar 2008 07:24 PM
my VBA code to calculate median values is broken. :( =?Utf-8?B?bmFjaG9saWJyZQ==?= Microsoft Access 3 13th Apr 2007 05:20 PM
Calculate MEDIAN of Last x Rows in a Column Sam via OfficeKB.com Microsoft Excel Worksheet Functions 6 26th Nov 2006 06:22 PM
Can pivot tables be changed to calculate median values? =?Utf-8?B?Q0NhaW4=?= Microsoft Access Getting Started 1 22nd Nov 2005 10:17 PM
Calculate top 100 values in non-consecutive rows ZermaPersians Microsoft Excel Programming 3 7th Sep 2003 08:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:37 PM.