I'm creating a formula in Excel VBA of the form
myformula(cell1, cell2, range)
where the range looks something like the first 7 columns of this:
V1 D2 V2 D3 V3 D4 V4 RESULT
0.5 2 0.7
There will always be a value in the first column, which will be a value between 0 and 1 inclusive. All subsequent columns may be empty, but if there is a value in D2 there will also be a value in V2 and so on.
My formula goes in the RESULT column and involves summing the figures in the V columns but only if the preceding D column is filled. i.e. in this case it would calculate V1 + V2 but ignore V3 and V4.
The VB I have been using works with
if IsEmpty(D2)
to select the appropriate values and this works fine in most cases.
However, I also want to be able to apply this formula to cases where the V and D values are calculated by a formula. The formula is conditional, outputting either a number, or "" (something like IF(A2="","",3)) So the cell range looks identical but is no longer considered truly empty by VB.
I'm looking for ways to work around this. I realise that the result of a formula will never be truly empty but is there a way of making the contents of the cell "empty enough" for VB to understand it as empty? Alternatively is there a different condition I can use to pick up the empty cells - but I must be able to differentiate between zero values and empty cells.
ETA: I am struggling to implement Stepan1010's solution. This is what I'm doing - it may be wrong:
Public Function YEARAV(sdate As Date, edate As Date, yearrange As Range) As Variant
Dim v1 As Variant
Dim v2 As Variant
Dim v3 As Variant
Dim v4 As Variant
Dim b As Date
Dim c As Date
Dim d As Date
v1 = yearrange.Cells(1, 1)
v2 = yearrange.Cells(1, 3)
v3 = yearrange.Cells(1, 5)
v4 = yearrange.Cells(1, 7)
b = yearrange.Cells(1, 2)
c = yearrange.Cells(1, 4)
d = yearrange.Cells(1, 6)
total_days = edate - sdate
a = sdate
e = edate
If Range(yearrange.Cells(1, 6)).Value = vbNullString Then
d = edate
If Range(yearrange.Cells(1, 4)).Value = vbNullString Then
c = edate
If Range(yearrange.Cells(1, 2)).Value = vbNullString Then
b = edate
End If
End If
End If
YEARAV = ((b - a) * v1 + (c - b) * v2 + (d - c) * v3 + (e - d) * v4) / total_days
End Function
I've also tried just using If b = vbNullString etc as well.

Oroperator... e.g.If IsEmpty(D2) Or D2="" Then. Add as many Ors as you want to describe what you consider is "empty enough".