Change the ranges to suit, it assumes that R37 is a cell and it contains the year only.
VBA Code:
If Range("R37").Value <= Year(Range("C93")) Then _
Range("S33").Formula = Replace("=SUMPRODUCT(--(YEAR($C$32:$C$#)=R33),$Q$32:$Q$#)", "#", Range("T13").Value)
Thanks Mark. I believe I may have mislead you with my request. I’m going to try to explain again. My problem is I don’t know the correct words for each action but I will try.
I’m looking for a formula to be placed in cell S33 that returns the value of the formula if the value is greater than zero. However, if the value is <= 0 then display nothing.
Except for displaying nothing if the value is zero, your original formula did most of what I wanted:
VBA Code:
Range("S33").Formula = "=SUMPRODUCT(--(YEAR($C$32:$C$" & Range("T13").Value & ")=(R33)),$Q$32:$Q$" & Range("T13").Value & ")"
What I then do is copy the formula from S33 down a variable distant depending on how many rows are needed. This can vary from S40 to S2033. The cell S33 then changes to S34, S35, S36, etc.
This is where I was hoping the IF statement
If(Formula>0,Formula,””) where the formula is:
SUMPRODUCT(--(YEAR($C$32:$C$" & Range("T13").Value & ")=(R33)),$Q$32:$Q$" & Range("T13").Value & ")
Or Peter’s formula:
SUMPRODUCT(--(YEAR($C$32:$C$#)=R33),$Q$32:$Q$#)", "#", Range("T13").Value
Here is a snip-it of my existing code that is doing what I want but as I noted - very sloppy and in some cases maybe a bit ridiculous, but it works. Any suggestions here would be welcome.
Code:
Private Sub cmdTest_Click()
Application.ScreenUpdating = False
Range("R33:U133").ClearContents
'. . . . . . . . . . . . . . . . . . .
Range("R33") = "=Year(T22)"
'. . . . . . . . . . . . . . . . . . .
Range("R34") = "=R33 + 1"
'. . . . . . . . . . . . . . . . . . .
Range("R34").Select
Selection.Copy
Range("R34:R133").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("R32").Select
'. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Range("U33").Formula = "=SUMPRODUCT(--(YEAR($C$32:$C$" & Range("T13").Value & ")=(R33)),$Q$32:$Q$" & Range("T13").Value & ")"
'. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Range("U33").Select
Selection.Copy
Range("U33:U133").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Range("T24").Select
Selection.Copy
Range("S33").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[2]>0,RC[2],"""")"
'. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Range("S33").Select
Selection.Copy
Range("S33:S54").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("S32").Select
End Sub
And again – thanks. . .