Question

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!!!

Was it helpful?

Solution

The following will write out the results of the countif:

Option Explicit
Sub Stack()

Dim MySheet As Worksheet
Dim FoundRng As Range, RangeToSelect As Range
Dim LastRow As Long, xx As Long

'assign our sheet to avoid confusion
Set MySheet = ThisWorkbook.ActiveSheet

'locate the "Rating" cell
Set FoundRng = MySheet.Cells.Find(What:="Rating", LookAt:=xlWhole, MatchCase:=False)
If FoundRng Is Nothing Then
    MsgBox ("No matching cell found, exiting sub!")
    Exit Sub
End If

'determine the boundaries of the target range for populating the formula
LastRow = MySheet.Cells(50000, FoundRng.Column).End(xlUp).Row

'assign the target range
Set RangeToSelect = Range(MySheet.Cells(FoundRng.Row, FoundRng.Column), MySheet.Cells(LastRow, FoundRng.Column))

'write out the countif results
MySheet.Cells(LastRow + 4, FoundRng.Column).Select
For xx = 7 To 1 Step -1
    ActiveCell.Value = Application.WorksheetFunction.CountIf(RangeToSelect, xx)
    ActiveCell.Offset(1, 0).Select
Next xx

End Sub

OTHER TIPS

You could do this

Dim RangeToSelect AS Range

Set RangeToSelect = ActiveWorksheet.Range(Top,Bottom)

For xx = 7 To 1 Step -1
    ActiveCell.Value = Application.WorksheetFunction.CountIf(RangeToSelect,xx)
    ActiveCell.Offset(1, 0).Select
Next xx

Unless you really need the formula in the cell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top