This is easier than I thought. I think
In cell H1 type in this formula...
=MIN(40,SUM(A1:G1))
This will add all the cells from A1 to G1. If the sum of all of the cells
is more than 40, then H4 will output 40 instead of the sum. If F1 or G1 is
blank it will count that cell as 0. The Min function is just a clever way of
doing things; you could also do it like this...
=IF(SUM(A1:G1)<40,SUM(A1:G1),40)
This says if the sum is less than 40 output the sum, otherwise output 40.
Both options give exactly the same result. With the MIN Function you are
simply telling excell to choose the smaller of two numbers (one being 40, and
one being the sum of the list). In your example the output would be 40, in
this example the output is 39.
A1:8
B1:8
C1:8
D1:8
E1:4
F1:3
G1:0