DYNAMIC RANGE FOR CRITERIA in COUNTIF Function in VBA

VBAEnjoi

New Member
Joined
Sep 30, 2018
Messages
33
Want to copy COUNTIF Formula in a Column with Dynamic Row length
Unable to have the reference to Criteria dynamic, G4 in this case (highlighted in red,bold below).
When it's copied to next row it continues to stay as G4

Dim lr As Long
Dim strAd As String
Dim strFm As String




With ActiveSheet
lr = .Cells(.Rows.Count, "G").End(xlUp).Row
strAd = .Range("E4", .Cells(lr, 5)).Address(0, 0)
strFm = "=COUNTIF(" & strAd & ", G4 )"


End With
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi & welcome to MrExcel.
How are you putting the formula into the sheet?
 
Upvote 0
Can you please post the code?
Also when posting code please use code tags, the # icon in the reply window.
 
Upvote 0
Can you please post the code?
Also when posting code please use code tags, the # icon in the reply window.

###

Dim lr As Long
Dim strAd As String
Dim strFm As String

With ActiveSheet
lr = .Cells(.Rows.Count, "G").End(xlUp).Row
strAd = .Range("E4", .Cells(lr, 5)).Address(0, 0)
strFm = "=COUNTIF(" & strAd & ", G4 )"


End With

Range("H4").Formula = strFm




Range("H5").Formula = strFm
 
Upvote 0
Try
Code:
Dim lr As Long
Dim strAd As String
Dim strFm As String

With ActiveSheet
lr = .Cells(.Rows.Count, "G").End(xlUp).Row
strAd = .Range("E4", .Cells(lr, 5)).Address
strFm = "=COUNTIF(" & strAd & ", G4 )"


End With

Range("H4:H5").Formula = strFm
 
Upvote 0
Hey MrExcel MVP

That worked. Many thanks for the prompt revert

Could you explain what the Address expression does
And how can I make the ranges in the array variables instead of the current fixed
strAd = .Range("E4", .Cells(lr, 5)).Address
 
Upvote 0
What variables do you want to use?
 
Upvote 0
If you want them as relative addresses use
Code:
.Address(0,0)
 
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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