ray88’s diary

お仕事で困ったとき用の自分用の覚書

VBA 税込み金額から消費税を抽出する部品

ExcelVBA 目次 - ray88’s diary

Function ExtractTax(totalAmount As Double, taxRate As Double, roundUp As Boolean) As Double

    ' 税抜き金額を計算します
    Dim taxExcludedAmount As Double
    taxExcludedAmount = totalAmount / (1 + taxRate / 100)
    
    ' 税金額を計算します
    Dim taxAmount As Double
    taxAmount = totalAmount - taxExcludedAmount
    
    ' 第三引数に基づいて上に切り上げるか下に切り捨てるかを決定します
    If roundUp Then
        ExtractTax = Application.WorksheetFunction.RoundUp(taxAmount, 0)
    Else
        ExtractTax = Application.WorksheetFunction.RoundDown(taxAmount, 0)
    End If

End Function