I don't know of an Excel formula but you could build one if you want. What you are seeking is a weighted average of fuel cost since the fuel molecules purchased at different costs don't stay separated from each other and they don't burn in order that they were put in the tank.
If you had $1,500 of fuel (300 gal x $5/gal) and then added it to $2,400 of fuel (400 gal x $6/gal), then the average cost of the fuel in your tank is $5.57/gal. ($3,900/700).
The cost of your 500 gal trip is $2,786 (500 gal x $5.57/gal).