This is an ugly hack for cell D4 that will work for the first part of your problem but only if you have 1 spare column available at the very end of your data (ie you're not already using all of the cells to the very last column in Excel):
In the above formula, you would replace the reference to L4 and M4 with the actual last cell of data and 'spare cell' respectively. Note also that you use F4 instead of E4 as indicated by the green text in the above code.
The following should address your second problem:
Again, replace references to g4 with the last cell of your actual data.
Bookmarks