IF(cell=1,Countif(Range(R2:R11),"=1"),"0") in VBA

UseLessFuel

New Member
Joined
Dec 22, 2012
Messages
37
Hello,
I'm struggling with applying a formula down a column, with VBA. The formula in cell AT13 is:

=IF(AL12=1,COUNTIF(R2:R11,"=1"),"0")

which I can extend down to the last row of data, but I would like to use VBA, to automate it.

The code below does not work, and it looks like I may need to use Application.WorksheetFunction.CountIf("R3:R12", "=1"), but I am unsure how to go about it. Perhaps I need to use R1C1 method for the Range, or is there another way?

Code:
Sub Z1FlagsD30_2()


Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual


'Helper column in AT -checks status of the TEN previous D30 if Z1Last10=1


Dim lr As Long


lr = Cells(Rows.Count, "A").End(xlUp).Row


     With Range("AT13:AT" & lr)
            .Formula = "=IF(AL13=1,COUNTIF(R3:R12,"=2"),0)"
            .Value = .Value
        End With
      
        
 
Application.ScreenUpdating = True 'turn it back on
Application.Calculation = xlCalculationAutomatic


End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try
Code:
            .Formula = "=IF(AL13=1,COUNTIF(R3:R12,""=2""),0)"
You need to double-up on the quotes inside a formula in VBA
 
Upvote 0
Thank you both @Fluff and [Mention]shg[/Mention]. I had forgotten about the double quotes and didn't know about having no quotes inside! Both worked.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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