PC Review


Reply
Thread Tools Rate Thread

Average row of Data using VBA

 
 
Carlee
Guest
Posts: n/a
 
      3rd Feb 2008
Hi,
I am trying to average a row of data using VBA. The catch is that the row
may or may not contain blanks, and as such, i would like the averge
calculation to skip blank values in the calculation. This is what i have
tried, but it hasn't worked

Range("HA18").Value = Application.Average(Range("C18:AG18")).Value

Can you help me out?
--
Carlee
 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      3rd Feb 2008
> Range("HA18").Value = Application.Average(Range("C18:AG18")).Value
use
> Range("HA18").Value = Application.Average(Range("C18:AG18"))



--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Carlee" <(E-Mail Removed)> wrote in message
news:3A7C19E5-0863-4966-9F16-(E-Mail Removed)...
> Hi,
> I am trying to average a row of data using VBA. The catch is that the row
> may or may not contain blanks, and as such, i would like the averge
> calculation to skip blank values in the calculation. This is what i have
> tried, but it hasn't worked
>
> Range("HA18").Value = Application.Average(Range("C18:AG18")).Value
>
> Can you help me out?
> --
> Carlee


 
Reply With Quote
 
Carlee
Guest
Posts: n/a
 
      3rd Feb 2008
THanks this worked great. Is there a way to add to this code to skip blank
cells?
--
Carlee


"Don Guillett" wrote:

> > Range("HA18").Value = Application.Average(Range("C18:AG18")).Value

> use
> > Range("HA18").Value = Application.Average(Range("C18:AG18"))

>
>
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "Carlee" <(E-Mail Removed)> wrote in message
> news:3A7C19E5-0863-4966-9F16-(E-Mail Removed)...
> > Hi,
> > I am trying to average a row of data using VBA. The catch is that the row
> > may or may not contain blanks, and as such, i would like the averge
> > calculation to skip blank values in the calculation. This is what i have
> > tried, but it hasn't worked
> >
> > Range("HA18").Value = Application.Average(Range("C18:AG18")).Value
> >
> > Can you help me out?
> > --
> > Carlee

>
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      3rd Feb 2008
You could use this function...

Function vbaAverage(RangeIn As Range) As Double
Dim Total As Double
Dim Count As Long
Dim Cel As Range
For Each Cel In RangeIn
If Cel.Value <> "" Then
Total = Total + Cel.Value
Count = Count + 1
End If
Next
vbaAverage = Total / Count
End Function

and call it from within your code like this...

Range("HA18").Value = vbaAverage(Range("C18:AG18"))

Rick


"Carlee" <(E-Mail Removed)> wrote in message
news:3A7C19E5-0863-4966-9F16-(E-Mail Removed)...
> Hi,
> I am trying to average a row of data using VBA. The catch is that the row
> may or may not contain blanks, and as such, i would like the averge
> calculation to skip blank values in the calculation. This is what i have
> tried, but it hasn't worked
>
> Range("HA18").Value = Application.Average(Range("C18:AG18")).Value
>
> Can you help me out?
> --
> Carlee


 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      3rd Feb 2008
Maybe this slight variation that protects against a range of empty cells
would be better...

Function vbaAverage(RangeIn As Range) As Double
Dim Total As Double
Dim Count As Long
Dim Cel As Range
For Each Cel In RangeIn
If Cel.Value <> "" Then
Total = Total + Cel.Value
Count = Count + 1
End If
Next
If Count > 0 Then vbaAverage = Total / Count
End Function

Rick


"Rick Rothstein (MVP - VB)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> You could use this function...
>
> Function vbaAverage(RangeIn As Range) As Double
> Dim Total As Double
> Dim Count As Long
> Dim Cel As Range
> For Each Cel In RangeIn
> If Cel.Value <> "" Then
> Total = Total + Cel.Value
> Count = Count + 1
> End If
> Next
> vbaAverage = Total / Count
> End Function
>
> and call it from within your code like this...
>
> Range("HA18").Value = vbaAverage(Range("C18:AG18"))
>
> Rick
>
>
> "Carlee" <(E-Mail Removed)> wrote in message
> news:3A7C19E5-0863-4966-9F16-(E-Mail Removed)...
>> Hi,
>> I am trying to average a row of data using VBA. The catch is that the
>> row
>> may or may not contain blanks, and as such, i would like the averge
>> calculation to skip blank values in the calculation. This is what i have
>> tried, but it hasn't worked
>>
>> Range("HA18").Value = Application.Average(Range("C18:AG18")).Value
>>
>> Can you help me out?
>> --
>> Carlee

>


 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      3rd Feb 2008

The worksheet function DOES skip truly blank cells. Do you have some that
appear blank but aren't?
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Carlee" <(E-Mail Removed)> wrote in message
newsE9C4A8D-D828-49ED-A256-(E-Mail Removed)...
> THanks this worked great. Is there a way to add to this code to skip
> blank
> cells?
> --
> Carlee
>
>
> "Don Guillett" wrote:
>
>> > Range("HA18").Value = Application.Average(Range("C18:AG18")).Value

>> use
>> > Range("HA18").Value = Application.Average(Range("C18:AG18"))

>>
>>
>> --
>> Don Guillett
>> Microsoft MVP Excel
>> SalesAid Software
>> (E-Mail Removed)
>> "Carlee" <(E-Mail Removed)> wrote in message
>> news:3A7C19E5-0863-4966-9F16-(E-Mail Removed)...
>> > Hi,
>> > I am trying to average a row of data using VBA. The catch is that the
>> > row
>> > may or may not contain blanks, and as such, i would like the averge
>> > calculation to skip blank values in the calculation. This is what i
>> > have
>> > tried, but it hasn't worked
>> >
>> > Range("HA18").Value = Application.Average(Range("C18:AG18")).Value
>> >
>> > Can you help me out?
>> > --
>> > Carlee

>>
>>


 
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
average of data new kid Microsoft Access 1 27th Apr 2009 04:04 AM
How do I average hourly data into daily data? Lindsey Microsoft Excel Misc 1 30th Mar 2009 05:05 PM
Average when No Data =?Utf-8?B?Y2FybA==?= Microsoft Excel Worksheet Functions 4 13th Jan 2006 07:39 PM
How do you average if your data contains a 0? =?Utf-8?B?cmFzNzEx?= Microsoft Excel Worksheet Functions 14 8th Nov 2005 03:55 AM
Trying to aggregate weekly data into monthly average data Karen 1144 Microsoft Excel Misc 4 6th Nov 2003 12:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:23 AM.