Question

I'm quite new to excel and have been trying to get this Countif formula to work for a while now. I want it to count from the 12th row in column AN p till the last used row. I am very close now but when I run the macro it gives me a REF error.

Sub Date1()
'
' Enter Date
'

Range("B15") = InputBox("Enter Date")


Dim LR As Long
LR = Sheets("Design Risk Scoring Sheet").Range("AN" & Rows.count).End(xlUp).Row

Range("B16").FormulaR1C1 = _
"=COUNTIF('Design Risk Scoring Sheet'!R[-4]C[38]:RC[38](" & LR & "), ""<"" & R[-1]C )"

End Sub

This is what i get in the formula cell when I run the macro

=COUNTIF('Design Risk Scoring Sheet'!AN12:AN16(163), "<" & B15 )

It should ideally be AN163 instead of 16. I have tried removing RC[38] and putting AN instead but i get AN(163) which gives a #NAME error and if i remove the brackets in (" & LR & ") then I get single quotation marks in the formula :

=COUNTIF('Design Risk Scoring Sheet'!AN12:'AN163', "<" & B15 )

I dont know how to fix this problem?

Was it helpful?

Solution

Alternate:

Sub Date1()

    Dim sDate As String

    sDate = InputBox("Enter Date", "Date Entry", Format(Now, "m/d/yyyy"))
    If Len(sDate) = 0 Then Exit Sub 'Pressed cancel

    If Not IsDate(sDate) Then
        MsgBox "[" & sDate & "] is not a valid date.", , "Exiting Macro"
        Exit Sub
    End If

    Range("B15").Value2 = DateValue(sDate)
    Range("B16").Formula = "=COUNTIF(AN12:AN" & Cells(Rows.Count, "AN").End(xlUp).Row & ",""<""&B15)"

End Sub

OTHER TIPS

Try This..

Range("B16").FormulaR1C1 = _
"=COUNTIF('Design Risk Scoring Sheet'!R[-4]C[38]:RC[38](" & LR & "), < & R[-1]C )"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top