Solved

Excel Frequency function problem

Posted on 1998-12-04
5
358 Views
Last Modified: 2008-02-26
I am trying to build a macro, that uses the Frequency function in Excel, and a table of predefined values. I want the function to give me the correct values in Excel. E.g.

If Cells A1 to A8 contain the data {1,1,2,2,2,2,2,4} and cells B1 to B4 contain the ranges the result would be:

Ranges   Result
1           2
2           5
3           0
4           1

How is this implemented in VBA as a macro???
0
Comment
Question by:markatatos
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:cartti
ID: 1614840
You can use an Excel function called COUNTIF. Syntax:- =COUNTIF(range,condition).
Using your example:

Cell C1 = "=COUNTIF(A1:A8,"=1")
Cell C2 = "=COUNTIF(A1:A8,"=2")

And so on and so on.


Using a vba macro, you could do the following:

Sub Add_Fequencies()
   dim count_range as string
   dim count_freq as integer
   dim plot_column as integer

   count_range = "A1:A8"
   count_freq = 4
   plot_column = 3

   for counter = 1 to count_freq
       cells(counter,plot_column-1).value = counter
       cells(counter, plot_column).formula = "=COUNTIF(" & _
count_range & ",""=" & counter & """)"
   next counter

End Sub
0
 

Author Comment

by:markatatos
ID: 1614841
What I am actually looking for is a way of using the predefined FREQUENCY function and not a custom one.

I need a macro that will set the answer in the cells as I noted before. The function you sent me is restricted to 1 to 8 and I dont want that(neither restrict it to column A). Additionally I need to use the predefined Freq function.
0
 
LVL 3

Accepted Solution

by:
frazer earned 50 total points
ID: 1614842
Hi,

Enter this code into a module.....

Function MyFrequency(TheRange As Range, TheValue As String) As Long
    Dim a As Range
    Dim Counter As Long
    Counter = 0
    For Each a In TheRange
        If a.Text = TheValue Then
            Counter = Counter + 1
        End If
    Next
    MyFrequency = Counter
End Function


then put the one of the following functions into your spreadsheet...

=MyFrequency(A1:A8,A1)

or

=MyFrequency(A1:A8,"4")

The first variable must be a range, the second can be either a range or a string.

Hope this helps

Frazer
0
 
LVL 2

Expert Comment

by:cartti
ID: 1614843
Why do you need a macro when the Frequency function on its own does what you want anyway. By what you said, you should reject Frazer's answer based on your principles (despite both our macros providing the results).
0
 

Author Comment

by:markatatos
ID: 1614844
Thanx, although I have been searching a way to use the predefined FREQUENCY function, time does not allow me to wait for any more answers. Anyway, cartti deserves those points as well, but it's true that a function is a more elegant way of the two, for achieving the task...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some time ago I was asked to create a VBA function that would calculate a check digit for an input number, using the following procedure: First, sum up all the individual digits in the number If that sum value has more than one digit, then sum up …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now