PC Review


Reply
Thread Tools Rate Thread

Calculation on a range problem

 
 
Andrew Bourke
Guest
Posts: n/a
 
      5th Jun 2008
Hi
Need a little help please.


Cells C3 to C7 contain these values.
The e indicates an estimated score and so needs to remain with the number.

45
73
50e
29
88

After some help from Bob Phillips, I got Avv2 to work.

Sub Avv2()
Dim rng As Range
Range("D37") = Application.Substitute(Range("C3:C7"), "e", "")
Set rng = Range("D37")
[C9] = Application.Average(Range("D37"))
[D9] = Application.Average(rng)
End Sub


But I can't get any of the following lines of code to work in Avv1.
The first two lines with rng cause compiler errors whilst the last line
[C9] =
doesn't give a compiler error but returns #DIV/0! in [C9].


Sub Avv1()
Dim rng As Range
Set rng = Range(Application.Substitute(Range("D37"), "e", ""))

rng = Range(Application.Substitute(Range("D37"), "e", ""))

[C9] = Application.Average(Application.Substitute(Range("C3:C7"), "e",""))
End Sub

I'm just trying to find the average for the list of numbers and I would
like to do this without having to copy them stripped down somewhere else
first.

TIA

Andrew



 
Reply With Quote
 
 
 
 
Ron Rosenfeld
Guest
Posts: n/a
 
      5th Jun 2008
On Thu, 05 Jun 2008 20:18:17 +0800, Andrew Bourke <(E-Mail Removed)> wrote:

>Hi
>Need a little help please.
>
>
>Cells C3 to C7 contain these values.
>The e indicates an estimated score and so needs to remain with the number.
>
>45
>73
>50e
>29
>88
>
>After some help from Bob Phillips, I got Avv2 to work.
>
>Sub Avv2()
>Dim rng As Range
>Range("D37") = Application.Substitute(Range("C3:C7"), "e", "")
>Set rng = Range("D37")
>[C9] = Application.Average(Range("D37"))
>[D9] = Application.Average(rng)
>End Sub
>
>
>But I can't get any of the following lines of code to work in Avv1.
>The first two lines with rng cause compiler errors whilst the last line
>[C9] =
>doesn't give a compiler error but returns #DIV/0! in [C9].
>
>
>Sub Avv1()
>Dim rng As Range
>Set rng = Range(Application.Substitute(Range("D37"), "e", ""))
>
>rng = Range(Application.Substitute(Range("D37"), "e", ""))
>
>[C9] = Application.Average(Application.Substitute(Range("C3:C7"), "e",""))
>End Sub
>
>I'm just trying to find the average for the list of numbers and I would
>like to do this without having to copy them stripped down somewhere else
>first.
>
>TIA
>
>Andrew
>
>


You could use an array worksheet formula:

=AVERAGE(--LEFT(rng,LEN(rng)-IF(NOT(ISNUMBER(rng)),1)))

To enter an array formula, hold down <ctrl><shift> while hitting <enter>. Excel
will place braces {...} around the formula.

If this must be done in VBA, then:

============================
Option Explicit
Function AvgRng(rng As Range) As Double
Dim c As Range
Dim dSum As Double
For Each c In rng
dSum = dSum + Val(c.Value)
Next c

AvgRng = dSum / rng.Count

End Function
==============================
--ron
 
Reply With Quote
 
Andrew Bourke
Guest
Posts: n/a
 
      6th Jun 2008
Thank you.

Ron Rosenfeld wrote:
> On Thu, 05 Jun 2008 20:18:17 +0800, Andrew Bourke <(E-Mail Removed)> wrote:
>
>> Hi
>> Need a little help please.
>>
>>
>> Cells C3 to C7 contain these values.
>> The e indicates an estimated score and so needs to remain with the number.
>>
>> 45
>> 73
>> 50e
>> 29
>> 88
>>
>> After some help from Bob Phillips, I got Avv2 to work.
>>
>> Sub Avv2()
>> Dim rng As Range
>> Range("D37") = Application.Substitute(Range("C3:C7"), "e", "")
>> Set rng = Range("D37")
>> [C9] = Application.Average(Range("D37"))
>> [D9] = Application.Average(rng)
>> End Sub
>>
>>
>> But I can't get any of the following lines of code to work in Avv1.
>> The first two lines with rng cause compiler errors whilst the last line
>> [C9] =
>> doesn't give a compiler error but returns #DIV/0! in [C9].
>>
>>
>> Sub Avv1()
>> Dim rng As Range
>> Set rng = Range(Application.Substitute(Range("D37"), "e", ""))
>>
>> rng = Range(Application.Substitute(Range("D37"), "e", ""))
>>
>> [C9] = Application.Average(Application.Substitute(Range("C3:C7"), "e",""))
>> End Sub
>>
>> I'm just trying to find the average for the list of numbers and I would
>> like to do this without having to copy them stripped down somewhere else
>> first.
>>
>> TIA
>>
>> Andrew
>>
>>

>
> You could use an array worksheet formula:
>
> =AVERAGE(--LEFT(rng,LEN(rng)-IF(NOT(ISNUMBER(rng)),1)))
>
> To enter an array formula, hold down <ctrl><shift> while hitting <enter>. Excel
> will place braces {...} around the formula.
>
> If this must be done in VBA, then:
>
> ============================
> Option Explicit
> Function AvgRng(rng As Range) As Double
> Dim c As Range
> Dim dSum As Double
> For Each c In rng
> dSum = dSum + Val(c.Value)
> Next c
>
> AvgRng = dSum / rng.Count
>
> End Function
> ==============================
> --ron

 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      6th Jun 2008
On Fri, 06 Jun 2008 07:16:23 +0800, Andrew Bourke <(E-Mail Removed)> wrote:

>Thank you.


You're welcome. Glad to help.
--ron
 
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
Need help with range value calculation rda Microsoft Excel Worksheet Functions 5 7th Apr 2008 03:25 AM
highlight range, apply calculation to data in cells and paste special to same range S Himmelrich Microsoft Excel Discussion 2 6th Nov 2007 11:48 PM
Setting Range by calculation =?Utf-8?B?ZXhwZWN0X2Vk?= Microsoft Excel Programming 3 23rd Jul 2007 07:32 PM
Time Range Calculation? Bdog Microsoft VB .NET 11 8th Jun 2005 05:51 PM
range.calculation with UDF not working when calculation is set to automatic Brian Murphy Microsoft Excel Programming 5 14th Oct 2003 07:02 PM


Features
 

Advertising
 

Newsgroups
 


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