上下水道関連部署で使えなそうなコードになります。
100を4等分すると25ですが、101の場合は少数になります。
それを回避して、26、25、25、25にならします。
102の場合、26、26、25、25にならします。
エクセル関数を駆使すればできそうですが、ぱっと思いつかないのでコードを書くことにしました。
基本エクセルの機能で完結する場合はマクロを使わない方が絶対にいいのですが、個人の作業で、調べるのが面倒で、コードなら秒で書けそうなことはマクロにします。
Sub Main()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim row As Long
For row = 2 To 100
Dim total As Long
If IsError(ws.Cells(row, 5).Value) Then
GoTo ErrorHandler
Else
total = ws.Cells(row, 5).Value
End If
' あまりによって場合分け
Select Case total Mod 4
Case 0
ws.Cells(row, 1).Value = total \ 4
ws.Cells(row, 2).Value = total \ 4
ws.Cells(row, 3).Value = total \ 4
ws.Cells(row, 4).Value = total \ 4
Case 1
ws.Cells(row, 1).Value = (total \ 4) + 1
ws.Cells(row, 2).Value = total \ 4
ws.Cells(row, 3).Value = total \ 4
ws.Cells(row, 4).Value = total \ 4
Case 2
ws.Cells(row, 1).Value = (total \ 4) + 1
ws.Cells(row, 2).Value = (total \ 4) + 1
ws.Cells(row, 3).Value = total \ 4
ws.Cells(row, 4).Value = total \ 4
Case 3
ws.Cells(row, 1).Value = (total \ 4) + 1
ws.Cells(row, 2).Value = (total \ 4) + 1
ws.Cells(row, 3).Value = (total \ 4) + 1
ws.Cells(row, 4).Value = total \ 4
End Select
ErrorHandler:
Next row
MsgBox "処理完了"
End SubIsErrorでエラーのセルは回避するようにしています。
縦の繰り返しは2行目から100行目までにしていいるのは楽をするためです。
100行目までやっときゃいいでしょ!とういう感じです。
ていねいに書くのであれば、Do Whileを使って「5列目が空白になるまで」とするべきでしょう。
Do While ws.Cells(row, 5).Value <> ""
Loopですね。
想定しているエクセル
下の画像のようなものを想定しています。
4か月分の合計が与えられていて、それを整数でうまく按分します。

実行すると

こうなります。











コメントを残す