Option Explicit
Sub test()
Dim strF As String, c As Integer, r As Integer
With Sheets("List1")
c = .[b6].CurrentRegion.Columns.Count: r = .[b6].CurrentRegion.Rows.Count
' .Cells(6, c + 1) = "total"
' .Cells(r + 6 + 1, 1) = "inv"
' .Range(.Cells(r + 6 + 1, 2), .Cells(r + 6 + 1, c)) = "=RANDBETWEEN(100,500)"
' .Cells(r + 6 + 1, c + 1) = "=sum(" & Range(Cells(r + 6 + 1, 2), Cells(r + 6 + 1, c)).Address & ")"
strF = "=IF(SUM(" & Range(Cells(7, 2), Cells(7, c)).Address(False) & _
")=0,"""",SUMPRODUCT(" & Range(Cells(7, 2), Cells(7, c)).Address(False) & _
"," & Range(.Cells(r + 6 + 1, 2), .Cells(r + 6 + 1, c)).Address & ")/" & Cells(r + 6 + 1, c + 1).Address & ")"
.Range(.Cells(7, c + 1), .Cells(r + 6 - 1, c + 1)).Formula = strF
End With
End Sub