CountIF within a for loop

Am1

New Member
Joined
Dec 6, 2019
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hi,
i need to use CountIf function inside a for loop, in VBA. I am very new to VBA, so sorry for trivial question.
Column "A" contains cells with text, with several duplicates.
For each cell in "A" column, i want to have in column "B" how many times text is present in the entire column "A"
I use code below, which is always returning "0" ...i think there is an error in how i am indexing cells in the for loop, inside COUNTIF function

For i = 2 To 100
Worksheets("Sheet1").Cells(i, 2).Formula = "=COUNTIF(A:A,cells(i,1).value)"
Next i

any ideas on where is my error?
thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
"=COUNTIF(A:A,cells(i,1).value)"
"=COUNTIF(A:A," & cells(i,1).value & ")"

Checkout special cells

Range("A:A").SpecialCells(xlCellTypeConstants, 23).Offset(, 1) = "=COUNTIF(A:A,A1)"
 
Last edited:
Upvote 0
You dont need a loop here. Use this:

VBA Code:
Worksheets("Sheet1").Range("B2:B100").FormulaR1C1 = "=COUNTIF(C[-1],RC[-1])"

FWIW your code should have been like this:

Code:
Worksheets("Sheet1").Cells(i, 2).Formula = "=COUNTIF(A:A," & Worksheets("Sheet1").Cells(i, 1).Value & ")"
 
Upvote 0
Sucks having a 10 minute limit for editing.

Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row) = "=COUNTIF(A:A,A1)"
 
Upvote 0
Thank you all, will take some time to understand and test your valuable inputs :)
no doubt they will all work
I was looking on google the meaning on this syntax , not able to find a clear explanation
" & cells(i,1).value & "
which is the meaning of thisn double "&"?
thanks!
 
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,109
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top