Solved

Excel Frequency function problem

Posted on 1998-12-04
5
362 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Outlook Free & Paid Tools
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

821 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