Macro code for Count If function

sanz

Board Regular
Joined
Jul 19, 2011
Messages
58
Hi All,

I have a macro recorded for the counif function, but the range selected is for static (ie B2:B8), i want this macro to be tweaked for dynamic active range selection(ie B2 till last active cell).

Sub Macro4()
'
' Macro4 Macro
'
' Keyboard Shortcut: Ctrl+t
'
ActiveCell.FormulaR1C1 = "=COUNTIF(RC[1]:R[6]C[1],RC[1])"
Range("B3").Select
Selection.End(xlDown).Select
Range("A8").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("A4").Select
End Sub


Any help would be appreciated

Tried to source and assemble the code, but this doesn't seem to work. Here the range value is static which need to be added by one untill last cell

Sub foo()

Dim lastRowColumnB As Long, var2 As String
lastRowColumnB = Range("B65000").End(xlUp).Row
var2 = Range("B2").Value

For i = 2 To lastRowColumnB
Cells(i, 1) = Application.CountIf(Range("B$2:B" & Cells(Rows.Count, 2).End(xlUp).Row), var2)
Next
End Sub



Regards,
Sanz
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Don't have excel at the moment....but
Code:
Sub Macro4()
Dim lr As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("A2:A" & lr).Formula = "=COUNTIF($B$2:B" & lr & ",B2)"
End Sub
 
Upvote 0
Sanz, you used the terms "Selection" and "last active cell". Are you really talking about a selected range rather than all rows on the sheet? If so, that would be in conflict with the fact that you want the formula in the active cell. In which case you would want to have the code run in stages:


Code:
Dim RngInput as Range
Dim rngCellsLike as Range
Set RngInput = Application.InputBox("Select the cells to count:","Range Selection",,,,,,8)
Set RngCellsLike = Application.InputBox("Which Cell to Compare To:","Cell Selection",,,,,,8)

ActiveCell.Formula = "=COUNTIF(" & RngInput.Address &"," & RngCellsLike.Address &")"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

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