Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default creating a small array from spreadsheet values/counts

Hi,

I need to create an array in VBA and then perform a MEDIAN function on
it.

The data in the sheet which will be used to create the array is as
follows:

A B
2 1
3 2
1 3
2 4
3 5

Column B refers to a VALUE, Column A refers to the COUNT of the number
of instance of that value in the desired array.

ie the array (1-dimensional list really) actually looks like:

1
1
2
2
2
3
4
4
5
5
5

What i am trying to do is create a FUNCTION which uses the ranges in
columns A&B to create this array and the return the MEDIAN of the array
back (in this small example, the MEDIAN is 3).

Any ideas please?

Thanks in Advance,

Tigpup

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default creating a small array from spreadsheet values/counts

Hi tigpup,
Try this function. The two ranges are inputted as one, so the left
column has to be the frequencies and the right column the values as in
your example data.

Public Function MEDIAN2(DataArray As Range) As Single
Application.Volatile
Dim MedianArray() As Single
Dim I As Integer
Dim J As Integer
Dim K As Long
Dim iArrayRows As Long
Dim iDataRows As Long
Dim iSheetRows As Long
iSheetRows = DataArray.Rows.Count
iArrayRows = WorksheetFunction.Sum(DataArray. _
Range(Cells(1, 1), Cells(iSheetRows, 1)))
ReDim Preserve MedianArray(1 To iArrayRows)
For I = 1 To iSheetRows
For J = 1 To DataArray.Cells(I, 1).Value
K = K + 1
MedianArray(K) = DataArray.Cells(I, 2).Value
Next J
Next I
If (UBound(MedianArray) Mod 2) = 0 Then
Let MEDIAN2 = (MedianArray(UBound(MedianArray) / 2) _
+ MedianArray(UBound(MedianArray) / 2 + 1)) / 2
Else: Let MEDIAN2 = MedianArray(UBound(MedianArray) / 2 + 0.5)
End If
End Function

I couldn't call it MEDIAN, that name clashed with the standard MEDIAN
function.

Ken Johnson

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default creating a small array from spreadsheet values/counts

If you go back to basics, the definition of median is the value that has 1/2
the population on either side of it. So, what you need to do is a running
total of the values in A until you get to SUM({col. A values})/2

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article .com,
says...
Hi,

I need to create an array in VBA and then perform a MEDIAN function on
it.

The data in the sheet which will be used to create the array is as
follows:

A B
2 1
3 2
1 3
2 4
3 5

Column B refers to a VALUE, Column A refers to the COUNT of the number
of instance of that value in the desired array.

ie the array (1-dimensional list really) actually looks like:

1
1
2
2
2
3
4
4
5
5
5

What i am trying to do is create a FUNCTION which uses the ranges in
columns A&B to create this array and the return the MEDIAN of the array
back (in this small example, the MEDIAN is 3).

Any ideas please?

Thanks in Advance,

Tigpup


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default creating a small array from spreadsheet values/counts

'--------------------------
Sub CreateNumbers()
Dim rngSelection As Excel.Range
Dim rngCell As Excel.Range
Dim lngResult As Double
Set rngSelection = Excel.Selection

For Each rngCell In rngSelection.Columns(2).Cells
rngCell.Resize(1, rngCell(1, 0).Value).Value = rngCell.Value
Next
lngResult = Application.Median(rngSelection.Columns(2).Resize _
(, Application.Max(rngSelection.Columns(1))))
MsgBox lngResult
End Sub
'------------------------
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware




"tigpup." wrote in message
Hi,
I need to create an array in VBA and then perform a MEDIAN function on
it.
The data in the sheet which will be used to create the array is as
follows:

A B
2 1
3 2
1 3
2 4
3 5

Column B refers to a VALUE, Column A refers to the COUNT of the number
of instance of that value in the desired array.
ie the array (1-dimensional list really) actually looks like:

1
1
2
2
2
3
4
4
5
5
5

What i am trying to do is create a FUNCTION which uses the ranges in
columns A&B to create this array and the return the MEDIAN of the array
back (in this small example, the MEDIAN is 3).
Any ideas please?
Thanks in Advance,
Tigpup
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default creating a small array from spreadsheet values/counts

Ken,

This is fantastic - many thanks.

It has also renewed my interest in VBA coding - i'll read the code
carefully and SLOWLY try to make full sense of it.

This is really appreciated.

Best Regards,

Tigpup



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default creating a small array from spreadsheet values/counts

Hi Tigpup,
You're welcome. Thanks for the feedback.
I guess you're aware that the result of the function is only correct
when column B (values) are in order (ascending or descending)
Ken Johnson

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,073
Default creating a small array from spreadsheet values/counts

Hi Tigpup,
Just an after-thought, I and J really should be dimensioned as Long, as
is K, just in case a user selects more than 32,767 rows ( the upper
limit for integer variable).

Ken Johnson

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default creating a small array from spreadsheet values/counts

Thanks to everyone for all your valuable help.

Issue has been resolved.

Regards,

Tigpup

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
Excel 2007 doesnt show Y-axis values when the values are small. outback Charts and Charting in Excel 2 October 26th 08 01:37 AM
Creating A Chart that counts repitition (don't know name) Firebird Charts and Charting in Excel 1 October 4th 08 01:51 PM
Formula That Counts First Value Only In Array As A Condition [email protected] Excel Worksheet Functions 3 February 4th 07 10:09 PM
Creating an column of values from an existing array PA New Users to Excel 1 November 20th 06 12:27 PM
Creating small "recordset" or sorting a numeric 1-dim array Anthony Cravero Excel Programming 0 December 19th 03 06:36 PM


All times are GMT +1. The time now is 10:59 AM.

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

About Us

"It's about Microsoft Excel"