How to use SLN function in VBA?

In VBA (Visual Basic for Applications), the SLN function is used to calculate the straight-line depreciation of an asset for one period. The straight-line method of depreciation assumes that the asset will depreciate by an equal amount each year over its useful life.

The SLN function in VBA has three arguments:

  1. Cost: The initial cost of the asset.
  2. Salvage: The value of the asset at the end of its useful life (also known as the salvage value).
  3. Life: The useful life of the asset (in periods, usually years).

Example

VBA

Sub CalculateDepreciation()
    Dim initialCost As Double
    Dim salvageValue As Double
    Dim lifeOfAsset As Double
    Dim yearlyDepreciation As Double
    
    ' Example values
    initialCost = 10000 ' The cost of the asset
    salvageValue = 1000  ' The salvage value of the asset
    lifeOfAsset = 5      ' The useful life of the asset (5 years)
    
    ' Calculate the yearly depreciation
    yearlyDepreciation = SLN(initialCost, salvageValue, lifeOfAsset)
    
    ' Output the result to the Immediate window (Ctrl+G to view)
    Debug.Print "The yearly depreciation is: " & yearlyDepreciation
End Sub

The Debug.Print statement will output the yearly depreciation to the Immediate Window in the VBA editor. If you want to use or display the value elsewhere (e.g., in a worksheet or a message box), you can assign it to a variable, or directly refer to the SLN function invocation in your VBA code.

Unlock Your Potential

Excel

Basic - Advanced

Access

Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project

Leave a Reply

Your email address will not be published. Required fields are marked *