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
 
If you want them as relative addresses use
Code:
.Address(0,0)


.Address(0,0)
Using Address(0,0) only impacts G4 in the formula.
The $E$4 doesn't change. Code below

###
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
###
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
To use code tags, do not use the # on the keyboard, but the # ICON in the reply window, just above where you type. Simply click the icon & then paste your code between the tags that appear.
The code you have just posted does not change the G4 reference, only the E4:E? reference.

Col H below is without the (0,0) & col I is withit


Excel 2013/2016
HI
400
500
asc
Cell Formulas
RangeFormula
H4=COUNTIF($E$4:$E$8, G4 )
H5=COUNTIF($E$4:$E$8, G5 )
I4=COUNTIF(E4:E8, G4 )
I5=COUNTIF(E5:E9, G5 )
 
Upvote 0
Got it.
Code:
on top of the message response.

Will check your response and revert.

Thanks for the prompt responses.

[/CODE]
To use code tags, do not use the # on the keyboard, but the # ICON in the reply window, just above where you type. Simply click the icon & then paste your code between the tags that appear.
The code you have just posted does not change the G4 reference, only the E4:E? reference.

Col H below is without the (0,0) & col I is withit

Excel 2013/2016
HI
400
500

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
asc

Worksheet Formulas
CellFormula
H4=COUNTIF($E$4:$E$8, G4 )
I4=COUNTIF(E4:E8, G4 )
H5=COUNTIF($E$4:$E$8, G5 )
I5=COUNTIF(E5:E9, G5 )

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,216,496
Messages
6,130,983
Members
449,611
Latest member
Bushra

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