Try this one:
Range("B9").Formula = "=COUNTIFS(Discharged!$E$2:$E$200," & """>=" & StartDate & """, Discharged!$E$2:$E$200, " & _
" ""<" & DateAdd("d", 1, StartDate) & """, Discharged!$H$2:$H$200, ""<24"")"
Frage
===
EDIT: The answer seems to highlight that this has more to do with my syntax foo than anything else
===
All I'm trying to do I put a formula in a cell. It's a formula that I developed before using VBA and now I need to automate the process.
When I run the code it's fine until it gets to this line.
Then I receive this error- Run-time error '1004': Application-defined or object-defined error
The code is contained in a loop but that is for context or for any school boy errors I've made.
Sheets("Data Summary").Activate
Dim startDate As Date
startDate = Now()
Dim i As Integer
For i = 1 To 7
startDate = DateAdd("d", 1, startDate)
cells.Range("B9").Formula = "=COUNTIFS(Discharged!$E$2:$E$200, " & _
" "">="" & startDate, " & _
" Discharged!$E$2:$E$200, " & _
" ""<"" & startDate.AddDays(1), " & _
" Discharged!$H$2:$H$200, " & _
" ""<24"" " 'here is the error
Next i
I've split up the formula into separate lines so that it's easier to read but it doesn't work if i put it all back on one line either. If I edit the formula so that there is a space before the = then the code runs but the formula doesn't get activated with the sheet, it just sits there as a value.
I've had a look at the FormulaLocal function and the FormulaR1C1 as well but it doesn't seem to achieve a different result.
Any help would be greatly appreciated.
Lösung
Try this one:
Range("B9").Formula = "=COUNTIFS(Discharged!$E$2:$E$200," & """>=" & StartDate & """, Discharged!$E$2:$E$200, " & _
" ""<" & DateAdd("d", 1, StartDate) & """, Discharged!$H$2:$H$200, ""<24"")"