Value of Cell = COUNTIF with changing criteria taken from list - VBA

CharlieRog

New Member
Joined
Sep 13, 2019
Messages
14
Looking to do a COUNTIF formula but paste it whilst using concatenation to change the criteria each time e.g

In column B I have a list of place names and in column C I would like it to show the number of times that place name is present in Column Q.

Would need to be setting the value of each cell in column C to be something like "=COUNTIF(Q2:Q1028,"" & MyLocationNameCell

Then it would move down to the next cell in column C and paste again but this time MyLocationNameCell would have a new value of next place name(taken from column B)

(if you were to use MyLocationNameCell as a variable)
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Does this do what you require? May have to change the sheet name and row references.

Code:
Dim x As Variant
Dim lr As Long

With Sheets("Sheet1")
    .Columns("C").ClearContents
    lr = .Range("B" & .Rows.Count).End(xlUp).Row
    Set x = .Range("B1:B" & lr)
    x = Evaluate("=IF(ROW(1:" & lr & "),COUNTIF('" & .Name & "'!Q2:Q1028,'" & .Name & "'!" & x.Address & "))")
    .Range("C1:C" & lr) = x
End With
 
Last edited:
Upvote 0
Does this do what you require? May have to change the sheet name and row references.

Code:
Dim x As Variant
Dim lr As Long

With Sheets("Sheet1")
    .Columns("C").ClearContents
    lr = .Range("B" & .Rows.Count).End(xlUp).Row
    Set x = .Range("B1:B" & lr)
    x = Evaluate("=IF(ROW(1:" & lr & "),COUNTIF('" & .Name & "'!Q2:Q1028,'" & .Name & "'!" & x.Address & "))")
    .Range("C1:C" & lr) = x
End With

**WORKS GREAT. Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,875
Messages
6,181,514
Members
453,050
Latest member
Obil

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