CALENDAR

このブログを検索

2011年8月31日水曜日

Excel Macro で工程表(Gantt Chart)のツール(完結編)

皆さん、こんばんは。

今回は、完結編として工程表でジグザク線を自動で書かせてみたいと思います。
一応、このシリーズはこれで終わりの予定です。

以下に、マクロのスクリプトを書いておきます。

Sub ZigzagLine()
'
' ZigzagLine Macro
'
Dim i As Integer
Dim SSD As Double
Dim SD As Double
Dim ED As Double
Dim p As Double
Dim Dulation As Double
Dim X1 As Double
Dim Y1 As Double
Dim X2 As Double
Dim Y2 As Double
Dim X3 As Double
Dim Y3 As Double
Dim a As Single
Dim colval As Single
Dim L As Double
If Not TypeName(Selection) = "Range" Then Exit Sub
i = Selection.Cells.Row
SD = Cells(i, 3).Value
ED = Cells(i, 4).Value
p = Cells(i, 9).Value
SSD = Cells(8, 10) 'To adjust with First Date Cell
X1 = Cells(8, 10).Left + (SD - SSD) * 25.5 + (ED - SD + 1) * p * 25.5
If X1 < Cells(8, 10).Left Then X1 = Cells(8, 10).Left a = 3 Else If X1 > Cells(8, 52).Left Then
X1 = Cells(8, 52).Left
a = 3
Else
a = 2
End If
End If
Y1 = Selection.Top + Selection.Height / a
X2 = Selection.Left
Y2 = Selection.Top
X3 = Selection.Left
Y3 = Selection.Top + Selection.Height
If Cells(i, 6).Value = "" Then Exit Sub
If Cells(8, 51).Value < Cells(i, 3).Value Then Exit Sub If a = 2 Then With ActiveSheet.Shapes.BuildFreeform(msoEditingAuto, X2, Y2) .AddNodes msoSegmentLine, msoEditingAuto, X1, Y1 .AddNodes msoSegmentLine, msoEditingAuto, X3, Y3 .ConvertToShape.Select End With Else With ActiveSheet.Shapes.BuildFreeform(msoEditingAuto, X2, Y2) .AddNodes msoSegmentLine, msoEditingAuto, X1, Y1 .AddNodes msoSegmentLine, msoEditingAuto, X1, Selection.Top + Selection.Height * 2 / a .AddNodes msoSegmentLine, msoEditingAuto, X3, Y3 .ConvertToShape.Select End With End If If X1 > X2 Then
colval = 12
Else
colval = 10
End If
Selection.ShapeRange.Line.Weight = 3.25
Selection.ShapeRange.Line.ForeColor.SchemeColor = colval
Cells(i + 1, 22).Activate 'To adjust with Next Start Date Cell
End Sub



【ちょっと解説】
C,D,Eの各列に予定工程の開始、終了、工数を書き込み、F,G,Hには実績工程のものを書き込んでおきます。

I列には進捗率を記入したうえで、ジグザグ線を記入したい行の管理日(ジグザグ線の始点となる日)を左側に置いたうえで上記のマクロを実行します。

進捗が遅れていれば赤線で、進んでいれば青線で、また、計画に対しての進捗率が表の中であれば三角に、工程表からはみ出るようであれば台形になるようになっています。

案の定、スパゲッティ・プログラムなので見難い(恥ずかスイ^^;)でしょうが、皆さんのそれぞれの工程表に合わせて、作り直してから使ってみて下さい。

今までの解説したサンプルファイルを置いておきますので、ここからダウンロードして各自使ってみて下さい。
何よりも、『習うより慣れろ』です。

それでは又、御機嫌よう…

0 件のコメント:

コメントを投稿