PC Review


Reply
Thread Tools Rate Thread

Applying Formula Based on Cell Contents

 
 
=?Utf-8?B?dmlsamE=?=
Guest
Posts: n/a
 
      8th Nov 2006
I have customer data that was collected over ten weeks. I need to be able to
calculate the averages of the data for customers that fall under a particular
category. Here are the steps in pseudocode:

while(not end of worksheet)
{
Test categoryCell
if categoryCell == condition
average data for this customer
increment to next categoryCell
}

So I need to know how to automate incrementing from one cell location to
another whithout modifying it's contents.
--
vilja
 
Reply With Quote
 
 
 
 
Sandy
Guest
Posts: n/a
 
      8th Nov 2006
Try this as a template, not sure where your data is to average so
you'll need to write that part maybe using
"Application.WorksheetFunction"

Sub LoopThruCells()
Dim CategoryCell As Range
For Each CategoryCell In Range("A1:A1") 'or whatever range
If CategoryCell.Value = "somthing" Then

'***average data code here***

End If
Next

End Sub

Sandy

vilja wrote:
> I have customer data that was collected over ten weeks. I need to be able to
> calculate the averages of the data for customers that fall under a particular
> category. Here are the steps in pseudocode:
>
> while(not end of worksheet)
> {
> Test categoryCell
> if categoryCell == condition
> average data for this customer
> increment to next categoryCell
> }
>
> So I need to know how to automate incrementing from one cell location to
> another whithout modifying it's contents.
> --
> vilja


 
Reply With Quote
 
=?Utf-8?B?dmlsamE=?=
Guest
Posts: n/a
 
      8th Nov 2006
Thanks Sandy. What if I need to change the data range to be averaged on the
fly. For example:
customer 1 meets the condition so his data will be included in the average,
customer 2 doesn't meet the condition so his data won't be included,
customers 5, 6 and 7 do meet the condition so their data will be included in
the average
etc.
--
vilja


"Sandy" wrote:

> Try this as a template, not sure where your data is to average so
> you'll need to write that part maybe using
> "Application.WorksheetFunction"
>
> Sub LoopThruCells()
> Dim CategoryCell As Range
> For Each CategoryCell In Range("A1:A1") 'or whatever range
> If CategoryCell.Value = "somthing" Then
>
> '***average data code here***
>
> End If
> Next
>
> End Sub
>
> Sandy
>
> vilja wrote:
> > I have customer data that was collected over ten weeks. I need to be able to
> > calculate the averages of the data for customers that fall under a particular
> > category. Here are the steps in pseudocode:
> >
> > while(not end of worksheet)
> > {
> > Test categoryCell
> > if categoryCell == condition
> > average data for this customer
> > increment to next categoryCell
> > }
> >
> > So I need to know how to automate incrementing from one cell location to
> > another whithout modifying it's contents.
> > --
> > vilja

>
>

 
Reply With Quote
 
Sandy
Guest
Posts: n/a
 
      8th Nov 2006
You can create an array and do a manual average, try playing around
with this:

Sub LoopThruCells_Average()
Dim CategoryCell As Range, mArray() As Variant
Dim MyAverage As Double
Dim n As Integer, i As Integer
n = 1
For Each CategoryCell In Range("A1:A10") 'or whatever range
If CategoryCell.Value > 0 Then
ReDim Preserve mArray(1 To n)
mArray(n) = CategoryCell.Value
n = n + 1
End If
Next
MsgBox UBound(mArray)
For i = 1 To UBound(mArray)
MyAverage = (mArray(i) + MyAverage)
Next i
MyAverage = MyAverage / UBound(mArray)
MsgBox MyAverage
End Sub

Sandy


vilja wrote:
> Thanks Sandy. What if I need to change the data range to be averaged on the
> fly. For example:
> customer 1 meets the condition so his data will be included in the average,
> customer 2 doesn't meet the condition so his data won't be included,
> customers 5, 6 and 7 do meet the condition so their data will be included in
> the average
> etc.
> --
> vilja
>
>
> "Sandy" wrote:
>
> > Try this as a template, not sure where your data is to average so
> > you'll need to write that part maybe using
> > "Application.WorksheetFunction"
> >
> > Sub LoopThruCells()
> > Dim CategoryCell As Range
> > For Each CategoryCell In Range("A1:A1") 'or whatever range
> > If CategoryCell.Value = "somthing" Then
> >
> > '***average data code here***
> >
> > End If
> > Next
> >
> > End Sub
> >
> > Sandy
> >
> > vilja wrote:
> > > I have customer data that was collected over ten weeks. I need to be able to
> > > calculate the averages of the data for customers that fall under a particular
> > > category. Here are the steps in pseudocode:
> > >
> > > while(not end of worksheet)
> > > {
> > > Test categoryCell
> > > if categoryCell == condition
> > > average data for this customer
> > > increment to next categoryCell
> > > }
> > >
> > > So I need to know how to automate incrementing from one cell location to
> > > another whithout modifying it's contents.
> > > --
> > > vilja

> >
> >


 
Reply With Quote
 
Sandy
Guest
Posts: n/a
 
      8th Nov 2006
You can create an array and do a manual average, try playing around
with this:

Sub LoopThruCells_Average()
Dim CategoryCell As Range, mArray() As Variant
Dim MyAverage As Double
Dim n As Integer, i As Integer
n = 1
For Each CategoryCell In Range("A1:A10") 'or whatever range
If CategoryCell.Value > 0 Then
ReDim Preserve mArray(1 To n)
mArray(n) = CategoryCell.Value
n = n + 1
End If
Next
MsgBox UBound(mArray)
For i = 1 To UBound(mArray)
MyAverage = (mArray(i) + MyAverage)
Next i
MyAverage = MyAverage / UBound(mArray)
MsgBox MyAverage
End Sub

Sandy


vilja wrote:
> Thanks Sandy. What if I need to change the data range to be averaged on the
> fly. For example:
> customer 1 meets the condition so his data will be included in the average,
> customer 2 doesn't meet the condition so his data won't be included,
> customers 5, 6 and 7 do meet the condition so their data will be included in
> the average
> etc.
> --
> vilja
>
>
> "Sandy" wrote:
>
> > Try this as a template, not sure where your data is to average so
> > you'll need to write that part maybe using
> > "Application.WorksheetFunction"
> >
> > Sub LoopThruCells()
> > Dim CategoryCell As Range
> > For Each CategoryCell In Range("A1:A1") 'or whatever range
> > If CategoryCell.Value = "somthing" Then
> >
> > '***average data code here***
> >
> > End If
> > Next
> >
> > End Sub
> >
> > Sandy
> >
> > vilja wrote:
> > > I have customer data that was collected over ten weeks. I need to be able to
> > > calculate the averages of the data for customers that fall under a particular
> > > category. Here are the steps in pseudocode:
> > >
> > > while(not end of worksheet)
> > > {
> > > Test categoryCell
> > > if categoryCell == condition
> > > average data for this customer
> > > increment to next categoryCell
> > > }
> > >
> > > So I need to know how to automate incrementing from one cell location to
> > > another whithout modifying it's contents.
> > > --
> > > vilja

> >
> >


 
Reply With Quote
 
Sandy
Guest
Posts: n/a
 
      8th Nov 2006
Try modding this code to your's:

Sub LoopThruCells_Average()
Dim CategoryCell As Range, mArray() As Variant
Dim MyAverage As Double
Dim n As Integer, i As Integer
n = 1
For Each CategoryCell In Range("A1:A10") 'or whatever range
If CategoryCell.Value > 0 Then
ReDim Preserve mArray(1 To n)
mArray(n) = CategoryCell.Value
n = n + 1
End If
Next
For i = 1 To UBound(mArray)
MyAverage = (mArray(i) + MyAverage)
Next i
MyAverage = MyAverage / UBound(mArray)
MsgBox MyAverage 'or use a range value
End Sub


HTH

Sandy

vilja wrote:
> Thanks Sandy. What if I need to change the data range to be averaged on the
> fly. For example:
> customer 1 meets the condition so his data will be included in the average,
> customer 2 doesn't meet the condition so his data won't be included,
> customers 5, 6 and 7 do meet the condition so their data will be included in
> the average
> etc.
> --
> vilja
>
>
> "Sandy" wrote:
>
> > Try this as a template, not sure where your data is to average so
> > you'll need to write that part maybe using
> > "Application.WorksheetFunction"
> >
> > Sub LoopThruCells()
> > Dim CategoryCell As Range
> > For Each CategoryCell In Range("A1:A1") 'or whatever range
> > If CategoryCell.Value = "somthing" Then
> >
> > '***average data code here***
> >
> > End If
> > Next
> >
> > End Sub
> >
> > Sandy
> >
> > vilja wrote:
> > > I have customer data that was collected over ten weeks. I need to be able to
> > > calculate the averages of the data for customers that fall under a particular
> > > category. Here are the steps in pseudocode:
> > >
> > > while(not end of worksheet)
> > > {
> > > Test categoryCell
> > > if categoryCell == condition
> > > average data for this customer
> > > increment to next categoryCell
> > > }
> > >
> > > So I need to know how to automate incrementing from one cell location to
> > > another whithout modifying it's contents.
> > > --
> > > vilja

> >
> >


 
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
Format based on cell contents being a formula or not Joe Microsoft Excel Worksheet Functions 2 11th Jul 2009 01:49 AM
Applying conditional formatting to cell based on another cell's in =?Utf-8?B?a2Rlc2VtcGxl?= Microsoft Excel Misc 1 22nd Mar 2006 06:37 PM
Lookup cell contents in on sheet based on a formula in second sheet Michael Wright via OfficeKB.com Microsoft Excel Worksheet Functions 1 30th Apr 2005 04:11 PM
Formula to return cell contents based on multiple conditions =?Utf-8?B?QmlsbA==?= Microsoft Excel Worksheet Functions 3 19th Jan 2005 09:59 AM
fix cell contents before deleting column from which formula's are based Erica Microsoft Excel Misc 2 9th Oct 2003 06:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 PM.