Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default I'm getting unwanted rounding to integer for an average of range of cells.

I'm trying to return an average value for a variable range of cells using this line of code:

rangeavg = WorksheetFunction.Average(Range(n1 & o1 & ":" & n1 & o2))

The values in the range have up to 3 decimal places, and the "rangeavg" variable is defined as "Long" but the routine keeps returning the average as an integer value. I need it to maintain the decimal place data. Am I missing something in this line of code or the variable definition or something?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default I'm getting unwanted rounding to integer for an average of range of cells.

wrote:
rangeavg = WorksheetFunction.Average(Range(n1 & o1 & ":" & n1 & o2))

The values in the range have up to 3 decimal places, and
the "rangeavg" variable is defined as "Long" but the routine
keeps returning the average as an integer value. I need it
to maintain the decimal place data.


Well, duh!, whadaya think type Long is?

Define rangeavg as type Double, as follows:

Dim rangeavg As Double

By the does not "maintain" the number of decimal places; that is, it is not
limited to 3 decimal places.

If you want to force rounding to 3 decimal places, you might do this one of
two ways:

rangeavg = Round(WorksheetFunction.Average(Range(n1 & o1 & ":" & n1 &
o2)),3)

rangeavg = WorksheetFunction.Round(WorksheetFunction.Average( Range(n1 & o1 &
":" & n1 & o2)),3)

There is a subtle difference between VBA Round and Excel Round. It is
demonstrated by the following example:

MsgBox Round(1.2345,3) & vbNewLine & WorksheetFunction.Round(1.2345,3)

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default I'm getting unwanted rounding to integer for an average of rangeof cells.

ahhhhhhh... I overlooked the fact that Long does not include decimals =P

Thanks!

Well, duh!, whadaya think type Long is?

Define rangeavg as type Double, as follows:

Dim rangeavg As Double

By the does not "maintain" the number of decimal places; that is, it is not
limited to 3 decimal places.

If you want to force rounding to 3 decimal places, you might do this one of
two ways:

rangeavg = Round(WorksheetFunction.Average(Range(n1 & o1 & ":" & n1 &
o2)),3)

rangeavg = WorksheetFunction.Round(WorksheetFunction.Average( Range(n1 & o1 &
":" & n1 & o2)),3)

There is a subtle difference between VBA Round and Excel Round. It is
demonstrated by the following example:

MsgBox Round(1.2345,3) & vbNewLine & WorksheetFunction.Round(1.2345,3)


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
average with out rounding up in a group of cells RWKCH Excel Worksheet Functions 2 September 20th 08 01:07 PM
Unwanted Rounding Bigjohn Excel Discussion (Misc queries) 2 May 25th 06 10:34 AM
help with rounding up to whole integer kjcramp Excel Discussion (Misc queries) 3 April 10th 06 07:00 PM
Rounding to nearest integer pattyh Excel Worksheet Functions 3 April 1st 05 08:02 PM
Unwanted Rounding Errors Anson[_2_] Excel Programming 1 June 10th 04 02:30 PM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"