There is a flaw in your logic
=IF(H3="";G3;IF(G3="";F3;IF(F3="";E3;IF(E3="";D3;I F(D3="";55;H3))))) always returns the value of G3 when H3 is empty, the value of G3 being LocD or 0.
If I understand correctly you need the content of the last non empty cell for a given SN
Try
Code:
=IF($D1="","",INDEX($D3:$H3,MATCH(REPT("z",255),$D3:$H3))