tigerlilly
Technical User
Hello,
I have posted this problem a few days ago now I am having problems with my calculation. I am not supposed to have negative numbers in my calculations and EOY value needs to be 0 when the last year is calculated. Depreciation remains constant though out. Also how do I get the totals to not display with any decimals? Thank you!!!
My code is below:
Option Explicit
'Detail column width constants
Private Const cfyearswidth As Long = 9
Private Const cfdeprwidth As Long = 11
Private Const cfEOYvaluewidth As Long = 11
Private Const cfEOYvalue1width As Long = 29
Private Const cfAccumDepwidth As Long = 11
Private fyears As Integer 'number of years
Private fdepr As Currency 'depreciation
Private feoyvalue As Currency 'end of year value
Private faccumdepr As Currency 'accumulated depreciation
Private fprice As Currency
Private fsvalue As Currency
'Purpose: When the Calculate button is clicked, it displays the Year, Depreciation, end of year
'value, and the accumulated depreciation.
Private Sub cmdCalculate_Click()
If IsNumeric(txtprice.Text) = True Then
If Val(txtprice.Text) > 0 Then
fprice = Val(txtprice.Text)
Else
MsgBox "Sorry, Price Value must be greater than zero"
txtprice.Text = ""
txtprice.SetFocus
Exit Sub
End If
Else
MsgBox "Sorry, Invalid data entered"
txtprice.Text = ""
txtprice.SetFocus
Exit Sub
End If
If IsNumeric(txtsvalue.Text) = True Then
If Val(txtsvalue.Text) <= fprice Then
fsvalue = Val(txtsvalue.Text)
Else
MsgBox "Sorry, Salvage Value must be less than price"
txtsvalue.Text = ""
txtsvalue.SetFocus
Exit Sub
End If
Else
MsgBox "Sorry, Invalid Data Entered"
txtsvalue.Text = ""
txtsvalue.SetFocus
Exit Sub
End If
If IsNumeric(txtterms.Text) = True Then
If Val(txtterms.Text) > 0 And Val(txtterms.Text) <= 30 Then
fyears = Val(txtterms.Text)
Else
MsgBox "Sorry, Years must be between 1 and 30"
txtterms.Text = ""
txtterms.SetFocus
Exit Sub
End If
Else
MsgBox "Sorry, Invalid Data Entered"
txtterms.Text = ""
txtterms.SetFocus
Exit Sub
End If
Call DisplayHeader
Call DisplayDetail
cmdCalculate.Enabled = False
End Sub
Private Sub cmdclear_Click()
txtprice.Text = ""
txtsvalue.Text = ""
txtterms.Text = ""
txtschedule.Text = ""
cmdCalculate.Enabled = True
End Sub
'Purpose: Terminates the program when the exit button is clicked.
Private Sub cmdexit_Click()
Unload frmMain
End Sub
'Purpose: Create a formated string containing year, depreciation, end of year value,
'Accumulated depreciation. Concatenate the string to the end of the txtschedule text property.
Private Sub DisplayDetail()
Dim I As Long
fprice = CCur(txtprice.Text)
fsvalue = CCur(txtsvalue.Text)
fyears = CCur(txtterms.Text)
'calculate and display detail
fdepr = (fprice - fsvalue) / fyears
For I = 1 To fyears
faccumdepr = faccumdepr + fdepr
If I <> fyears Then
feoyvalue = fprice - faccumdepr
Else
fdepr = (fprice - fsvalue - faccumdepr)
faccumdepr = (faccumdepr + fdepr)
feoyvalue = (feoyvalue - fdepr)
fdepr = feoyvalue
feoyvalue = 0
End If
'Build the formated detail line and add to the text box
txtschedule.Text = txtschedule.Text & vbNewLine & _
RightAlign(Format(I, "0"
, cfyearswidth) & _
RightAlign(Format(fdepr, "0.00"
, cfdeprwidth) & _
RightAlign(Format(feoyvalue, "0.00"
, cfEOYvaluewidth) & _
RightAlign(Format(faccumdepr, "0.00"
, cfAccumDepwidth)
Next I
End Sub
'Purpose: Create a formatted string containing two lines of header for the depreciation table.
'Assign the string to the text property of the txtschedule text box.
Private Sub DisplayHeader()
txtschedule.Text = RightAlign("EOY", cfEOYvalue1width) & _
RightAlign("Accum", cfAccumDepwidth) & vbNewLine & _
RightAlign("Year", cfyearswidth) & _
RightAlign("Depr", cfdeprwidth) & _
RightAlign("Value", cfEOYvaluewidth) & _
RightAlign("Depr", cfdeprwidth)
End Sub
'Purpose: Disable the Calculate button when the form is loaded
Private Sub Form_Load()
cmdCalculate.Enabled = True
End Sub
'Purpose: Right aligns a string
Public Function RightAlign(ByVal Source As String, _
ByVal Length As Integer) As String
Dim Result As String
Result = Space(Length)
RSet Result = Source
RightAlign = Result
End Function
I have posted this problem a few days ago now I am having problems with my calculation. I am not supposed to have negative numbers in my calculations and EOY value needs to be 0 when the last year is calculated. Depreciation remains constant though out. Also how do I get the totals to not display with any decimals? Thank you!!!
My code is below:
Option Explicit
'Detail column width constants
Private Const cfyearswidth As Long = 9
Private Const cfdeprwidth As Long = 11
Private Const cfEOYvaluewidth As Long = 11
Private Const cfEOYvalue1width As Long = 29
Private Const cfAccumDepwidth As Long = 11
Private fyears As Integer 'number of years
Private fdepr As Currency 'depreciation
Private feoyvalue As Currency 'end of year value
Private faccumdepr As Currency 'accumulated depreciation
Private fprice As Currency
Private fsvalue As Currency
'Purpose: When the Calculate button is clicked, it displays the Year, Depreciation, end of year
'value, and the accumulated depreciation.
Private Sub cmdCalculate_Click()
If IsNumeric(txtprice.Text) = True Then
If Val(txtprice.Text) > 0 Then
fprice = Val(txtprice.Text)
Else
MsgBox "Sorry, Price Value must be greater than zero"
txtprice.Text = ""
txtprice.SetFocus
Exit Sub
End If
Else
MsgBox "Sorry, Invalid data entered"
txtprice.Text = ""
txtprice.SetFocus
Exit Sub
End If
If IsNumeric(txtsvalue.Text) = True Then
If Val(txtsvalue.Text) <= fprice Then
fsvalue = Val(txtsvalue.Text)
Else
MsgBox "Sorry, Salvage Value must be less than price"
txtsvalue.Text = ""
txtsvalue.SetFocus
Exit Sub
End If
Else
MsgBox "Sorry, Invalid Data Entered"
txtsvalue.Text = ""
txtsvalue.SetFocus
Exit Sub
End If
If IsNumeric(txtterms.Text) = True Then
If Val(txtterms.Text) > 0 And Val(txtterms.Text) <= 30 Then
fyears = Val(txtterms.Text)
Else
MsgBox "Sorry, Years must be between 1 and 30"
txtterms.Text = ""
txtterms.SetFocus
Exit Sub
End If
Else
MsgBox "Sorry, Invalid Data Entered"
txtterms.Text = ""
txtterms.SetFocus
Exit Sub
End If
Call DisplayHeader
Call DisplayDetail
cmdCalculate.Enabled = False
End Sub
Private Sub cmdclear_Click()
txtprice.Text = ""
txtsvalue.Text = ""
txtterms.Text = ""
txtschedule.Text = ""
cmdCalculate.Enabled = True
End Sub
'Purpose: Terminates the program when the exit button is clicked.
Private Sub cmdexit_Click()
Unload frmMain
End Sub
'Purpose: Create a formated string containing year, depreciation, end of year value,
'Accumulated depreciation. Concatenate the string to the end of the txtschedule text property.
Private Sub DisplayDetail()
Dim I As Long
fprice = CCur(txtprice.Text)
fsvalue = CCur(txtsvalue.Text)
fyears = CCur(txtterms.Text)
'calculate and display detail
fdepr = (fprice - fsvalue) / fyears
For I = 1 To fyears
faccumdepr = faccumdepr + fdepr
If I <> fyears Then
feoyvalue = fprice - faccumdepr
Else
fdepr = (fprice - fsvalue - faccumdepr)
faccumdepr = (faccumdepr + fdepr)
feoyvalue = (feoyvalue - fdepr)
fdepr = feoyvalue
feoyvalue = 0
End If
'Build the formated detail line and add to the text box
txtschedule.Text = txtschedule.Text & vbNewLine & _
RightAlign(Format(I, "0"
RightAlign(Format(fdepr, "0.00"
RightAlign(Format(feoyvalue, "0.00"
RightAlign(Format(faccumdepr, "0.00"
Next I
End Sub
'Purpose: Create a formatted string containing two lines of header for the depreciation table.
'Assign the string to the text property of the txtschedule text box.
Private Sub DisplayHeader()
txtschedule.Text = RightAlign("EOY", cfEOYvalue1width) & _
RightAlign("Accum", cfAccumDepwidth) & vbNewLine & _
RightAlign("Year", cfyearswidth) & _
RightAlign("Depr", cfdeprwidth) & _
RightAlign("Value", cfEOYvaluewidth) & _
RightAlign("Depr", cfdeprwidth)
End Sub
'Purpose: Disable the Calculate button when the form is loaded
Private Sub Form_Load()
cmdCalculate.Enabled = True
End Sub
'Purpose: Right aligns a string
Public Function RightAlign(ByVal Source As String, _
ByVal Length As Integer) As String
Dim Result As String
Result = Space(Length)
RSet Result = Source
RightAlign = Result
End Function