You may try this Array Formula which requires confirmation with Ctrl+Shift+Enter instead of Enter alone.
=LOOKUP(2,1/D2:D1000,ROW(D2:D1000))-LOOKUP(2,1/IF(D2:D1000<>"",IF(E2:E1000<>"",(D2:D1000=E2:E1000))),ROW(D2:D1000))
Remember that an Array Formula is confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer.
Bookmarks