Object Variable Not Set while indexing

Function Interest_Declining_Balance(optional OB,optional pmt(),optional purchase(),optional Days() as integer, optional rate)
dim Balance as double
dim Balance_Due as double
dim Sum as integer
dim Interest as double
dim Months as double

    	for i = 1 to UBound(Days)
    		Balance = (OB + pmt(i,1)-purchase(i,1))*Days(i,1) 
    	next i 
    	for a = 1 to UBound(Days)
    		Sum = Sum + Days(a,1)
    	next a 
    	Balance_Due = Balance / Sum
    	Months = Sum/30
    	Interest = rate/100
    	Interest_Declining_Balance = (Balance_Due*Interest*Months)
    End Function

Please reformat into something more readable, tell us how you call that and ask a question.