I am trying to use write a macro using COUNTIF to find the number of times a number appears in a range. It is a list of ratings, from 1 to 7. The issue is that the number of rows will vary each time, as well as the column where the ratings will be.
I actually did write this successfully before, but I lost all my code when my hard drive crashed! So I know this can be done, but I don't remember how I did it. Here is my code with comments:
'find the cell called "Rating". In this example, it will be in $E$13
Cells.Find(What:="Rating", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=True).Activate
'This will be $E$13
Top = ActiveCell.Address
'This will be 5, for column E
CurrentColumn = ActiveCell.Column
'go to the bottom cell of the range
Cells(50000, CurrentColumn).End(xlUp).Select
'This will be $E$37
Bottom = ActiveCell.Address
'Combine the top and bottom to make the range, which will be $E$13:$E$37
RangeToSelect = Top & ":" & Bottom
'Under the range, go down 4 cells and do a COUNTIF for the numbers 7 to 1
ActiveCell.Offset(4, 0).Range("A1").Select
For xx = 7 To 1 Step -1
ActiveCell.FormulaR1C1 = "=COUNTIF(" & RangeToSelect & "," & xx & ")"
ActiveCell.Offset(1, 0).Range("A1").Select
Next xx
The code where it throws the error is:
ActiveCell.FormulaR1C1 = "=COUNTIF(" & RangeToSelect & "," & xx & ")"
It should end up being =COUNTIF($E$13:$E$37,7) for the first cell, then finding 6, 5,...1. Any help and/or suggestions would be greatly appreciated!!!