I think you need to give more info.
Why are you using a COUNTIF to search just the single cell B7? This would
better be done with a simple IF statement. I.e. your statement can be
replaced with
=IF(OR(B7="H",B7="B",B7="HD"),B6,0)
But what do you mean by "... occasionally the cells lose the references"?