VBAで金種を計算する方法

こんにちは!大阪市を拠点に活動している『縁紡ぐ』の稲垣です。

SWELLを使ったホームページ制作や、Excel、ACCESS、RPAなどのシステム開発を行っています。
また、Excel、Word、Outlookの研修や、情報セキュリティ研修も行っています。身近なITの相談相手になりたいと思っています。

お気軽にお問い合わせください。

目次

VBAで金種を計算する方法

今回は、VBAを使って指定した金額から必要な金種を計算する方法をご紹介します。VBA初心者の方でも簡単に理解できる内容ですので、ぜひ最後までお読みください。

目的

今回ご依頼があったお仕事の中で、指定した金額に基づいて必要な金種(紙幣や硬貨)の枚数を計算する場面があったので、VBAコードをご紹介します。金銭管理を効率化するための役立つツールとして、ぜひ活用してください。

ひとつのプロシージャで完結するVBAのコード

金種の枚数を簡単に計算できます。

Sub CalculateDenominations()
    ' 合計金額を格納する変数
    Dim totalAmount As Long
    ' 各金種の枚数を格納する変数
    Dim tenThousandYenCount As Long
    Dim fiveThousandYenCount As Long
    Dim twoThousandYenCount As Long
    Dim oneThousandYenCount As Long
    Dim fiveHundredYenCount As Long
    Dim oneHundredYenCount As Long
    Dim fiftyYenCount As Long
    Dim tenYenCount As Long
    Dim fiveYenCount As Long
    Dim oneYenCount As Long

    ' ユーザーから合計金額を入力してもらう
    totalAmount = InputBox("合計金額を入力してください:")

    ' 1万円札の枚数を計算
    tenThousandYenCount = totalAmount \ 10000
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 10000

    ' 5000円札の枚数を計算
    fiveThousandYenCount = totalAmount \ 5000
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 5000

    ' 2000円札の枚数を計算
    twoThousandYenCount = totalAmount \ 2000
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 2000

    ' 1000円札の枚数を計算
    oneThousandYenCount = totalAmount \ 1000
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 1000

    ' 500円玉の枚数を計算
    fiveHundredYenCount = totalAmount \ 500
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 500

    ' 100円玉の枚数を計算
    oneHundredYenCount = totalAmount \ 100
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 100

    ' 50円玉の枚数を計算
    fiftyYenCount = totalAmount \ 50
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 50

    ' 10円玉の枚数を計算
    tenYenCount = totalAmount \ 10
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 10

    ' 5円玉の枚数を計算
    fiveYenCount = totalAmount \ 5
    ' 残りの金額を更新
    totalAmount = totalAmount Mod 5

    ' 1円玉の枚数を計算
    oneYenCount = totalAmount ' 残りの金額が1円玉の枚数となる

    ' 計算結果をメッセージボックスで表示
    MsgBox "必要な金種の枚数は:" & vbCrLf & _
           "10000円札: " & tenThousandYenCount & " 枚" & vbCrLf & _
           "5000円札: " & fiveThousandYenCount & " 枚" & vbCrLf & _
           "2000円札: " & twoThousandYenCount & " 枚" & vbCrLf & _
           "1000円札: " & oneThousandYenCount & " 枚" & vbCrLf & _
           "500円玉: " & fiveHundredYenCount & " 枚" & vbCrLf & _
           "100円玉: " & oneHundredYenCount & " 枚" & vbCrLf & _
           "50円玉: " & fiftyYenCount & " 枚" & vbCrLf & _
           "10円玉: " & tenYenCount & " 枚" & vbCrLf & _
           "5円玉: " & fiveYenCount & " 枚" & vbCrLf & _
           "1円玉: " & oneYenCount & " 枚"
End Sub

クラスで作成した場合

クラスで作成した場合は、下記のコードになります。

Option Explicit

' 合計金額を保持する変数
Private totalAmount As Long

' 各金種の枚数を保持する変数
Private tenThousandYenCount As Long
Private fiveThousandYenCount As Long
Private twoThousandYenCount As Long
Private oneThousandYenCount As Long
Private fiveHundredYenCount As Long
Private oneHundredYenCount As Long
Private fiftyYenCount As Long
Private tenYenCount As Long
Private fiveYenCount As Long
Private oneYenCount As Long

' 合計金額を設定する
Public Sub SetTotalAmount(amount As Long)
    totalAmount = amount
    CalculateDenominations
End Sub

' 金種の枚数を計算するメソッド
Private Sub CalculateDenominations()
    ' 1万円札の枚数を計算
    tenThousandYenCount = totalAmount \ 10000
    totalAmount = totalAmount Mod 10000

    ' 5000円札の枚数を計算
    fiveThousandYenCount = totalAmount \ 5000
    totalAmount = totalAmount Mod 5000

    ' 2000円札の枚数を計算
    twoThousandYenCount = totalAmount \ 2000
    totalAmount = totalAmount Mod 2000

    ' 1000円札の枚数を計算
    oneThousandYenCount = totalAmount \ 1000
    totalAmount = totalAmount Mod 1000

    ' 500円玉の枚数を計算
    fiveHundredYenCount = totalAmount \ 500
    totalAmount = totalAmount Mod 500

    ' 100円玉の枚数を計算
    oneHundredYenCount = totalAmount \ 100
    totalAmount = totalAmount Mod 100

    ' 50円玉の枚数を計算
    fiftyYenCount = totalAmount \ 50
    totalAmount = totalAmount Mod 50

    ' 10円玉の枚数を計算
    tenYenCount = totalAmount \ 10
    totalAmount = totalAmount Mod 10

    ' 5円玉の枚数を計算
    fiveYenCount = totalAmount \ 5
    totalAmount = totalAmount Mod 5

    ' 1円玉の枚数を計算
    oneYenCount = totalAmount ' 残りの金額が1円玉の枚数となる
End Sub

' 各金種の枚数を取得する
Public Function GetTenThousandYenCount() As Long
    GetTenThousandYenCount = tenThousandYenCount
End Function

Public Function GetFiveThousandYenCount() As Long
    GetFiveThousandYenCount = fiveThousandYenCount
End Function

Public Function GetTwoThousandYenCount() As Long
    GetTwoThousandYenCount = twoThousandYenCount
End Function

Public Function GetOneThousandYenCount() As Long
    GetOneThousandYenCount = oneThousandYenCount
End Function

Public Function GetFiveHundredYenCount() As Long
    GetFiveHundredYenCount = fiveHundredYenCount
End Function

Public Function GetOneHundredYenCount() As Long
    GetOneHundredYenCount = oneHundredYenCount
End Function

Public Function GetFiftyYenCount() As Long
    GetFiftyYenCount = fiftyYenCount
End Function

Public Function GetTenYenCount() As Long
    GetTenYenCount = tenYenCount
End Function

Public Function GetFiveYenCount() As Long
    GetFiveYenCount = fiveYenCount
End Function

Public Function GetOneYenCount() As Long
    GetOneYenCount = oneYenCount
End Function

' 結果を文字列でまとめて取得するメソッド
Public Function GetDenominations() As String
    GetDenominations = "必要な金種の枚数は:" & vbCrLf & _
                       "10000円札: " & GetTenThousandYenCount & " 枚" & vbCrLf & _
                       "5000円札: " & GetFiveThousandYenCount & " 枚" & vbCrLf & _
                       "2000円札: " & GetTwoThousandYenCount & " 枚" & vbCrLf & _
                       "1000円札: " & GetOneThousandYenCount & " 枚" & vbCrLf & _
                       "500円玉: " & GetFiveHundredYenCount & " 枚" & vbCrLf & _
                       "100円玉: " & GetOneHundredYenCount & " 枚" & vbCrLf & _
                       "50円玉: " & GetFiftyYenCount & " 枚" & vbCrLf & _
                       "10円玉: " & GetTenYenCount & " 枚" & vbCrLf & _
                       "5円玉: " & GetFiveYenCount & " 枚" & vbCrLf & _
                       "1円玉: " & GetOneYenCount & " 枚"
End Function

' 金額を入力し、計算結果を表示する
Sub ShowDenominations()
    Dim amount As Long
    amount = InputBox("合計金額を入力してください:")
    
    Dim calculator As New DenominationCalculator
    calculator.SetTotalAmount amount
    
    ' 計算結果をメッセージボックスで表示
    MsgBox calculator.GetDenominations()
End Sub

関数型でグローバル変数に結果を代入

グローバル変数にすると、他のモジュールからも簡単に取得できるので便利です。

Option Explicit

' グローバル変数を定義
Public totalAmount As Long
Public tenThousandYenCount As Long
Public fiveThousandYenCount As Long
Public twoThousandYenCount As Long
Public oneThousandYenCount As Long
Public fiveHundredYenCount As Long
Public oneHundredYenCount As Long
Public fiftyYenCount As Long
Public tenYenCount As Long
Public fiveYenCount As Long
Public oneYenCount As Long

' 合計金額を設定する
Public Sub SetTotalAmount(amount As Long)
    totalAmount = amount
    CalculateDenominations
End Sub

' 金種の枚数を計算する
Private Sub CalculateDenominations()
    ' 1万円札の枚数を計算
    tenThousandYenCount = totalAmount \ 10000
    totalAmount = totalAmount Mod 10000

    ' 5000円札の枚数を計算
    fiveThousandYenCount = totalAmount \ 5000
    totalAmount = totalAmount Mod 5000

    ' 2000円札の枚数を計算
    twoThousandYenCount = totalAmount \ 2000
    totalAmount = totalAmount Mod 2000

    ' 1000円札の枚数を計算
    oneThousandYenCount = totalAmount \ 1000
    totalAmount = totalAmount Mod 1000

    ' 500円玉の枚数を計算
    fiveHundredYenCount = totalAmount \ 500
    totalAmount = totalAmount Mod 500

    ' 100円玉の枚数を計算
    oneHundredYenCount = totalAmount \ 100
    totalAmount = totalAmount Mod 100

    ' 50円玉の枚数を計算
    fiftyYenCount = totalAmount \ 50
    totalAmount = totalAmount Mod 50

    ' 10円玉の枚数を計算
    tenYenCount = totalAmount \ 10
    totalAmount = totalAmount Mod 10

    ' 5円玉の枚数を計算
    fiveYenCount = totalAmount \ 5
    totalAmount = totalAmount Mod 5

    ' 1円玉の枚数を計算
    oneYenCount = totalAmount ' 残りの金額が1円玉の枚数となる
End Sub

' 各金種の枚数を文字列で取得
Public Function GetDenominations() As String
    GetDenominations = "必要な金種の枚数は:" & vbCrLf & _
                       "10000円札: " & tenThousandYenCount & " 枚" & vbCrLf & _
                       "5000円札: " & fiveThousandYenCount & " 枚" & vbCrLf & _
                       "2000円札: " & twoThousandYenCount & " 枚" & vbCrLf & _
                       "1000円札: " & oneThousandYenCount & " 枚" & vbCrLf & _
                       "500円玉: " & fiveHundredYenCount & " 枚" & vbCrLf & _
                       "100円玉: " & oneHundredYenCount & " 枚" & vbCrLf & _
                       "50円玉: " & fiftyYenCount & " 枚" & vbCrLf & _
                       "10円玉: " & tenYenCount & " 枚" & vbCrLf & _
                       "5円玉: " & fiveYenCount & " 枚" & vbCrLf & _
                       "1円玉: " & oneYenCount & " 枚"
End Function

' ユーザーから金額を入力し、計算結果を表示する
Sub ShowDenominations()
    Dim amount As Long
    amount = InputBox("合計金額を入力してください:")
    
    SetTotalAmount amount
    
    ' 計算結果をメッセージボックスで表示
    MsgBox GetDenominations()
End Sub

2,000円が不要の場合

最近、2,000円札を見たことがありません💦2,000円札が不要の場合は、2,000円の部分をコメントアウトすると使えます。

まとめ

この記事では、VBAを使って金種を計算する方法を紹介しました。このコードを活用することで、日常の金銭管理がより効率的になります。ぜひ、実際に試してみてください!

記事を書いた人

稲垣

  • Excel、ACCESSでのシステム開発が得意
  • ITスキルを共有し実践的に学びながら成長する人を見るのが幸せ
  • 自家焙煎するほどのコーヒー好き
  • 使用言語 VBA、Python、Javascript、Java、HTML、CSS etc.
  • 保有資格 Kintoneアソシエイト、日商簿記検定2級、マンション管理士、管理業務主任者、情報セキュリティマネジメント、ExcelVBA etc.
  • 業務フロー図の作成や業務時間分析を通して、効率化ポイントを探る人
  • お客様にとって本当に良いことかを第一に考える人
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする

目次