CALENDAR

このブログを検索

2011年8月7日日曜日

Excel Macro で工程表(Gantt Chart)のツール(前回の答え)

皆さん、こんにちは。

前回では3つのツールをExcel Macro で作りましたが、その中で宿題を出しておりました。

今回は、その答え合わせをします。

回答は、1番目(選択範囲にバーを引くマクロ)と2番目のマクロ(作業開始日(Start Date)・終了日(Finish Date)からバーを引くマクロ)を改良しなければ、一つのセルが2日以上の時には使えない、というものです。

以下にそれぞれの正解マクロを書き出します。


● 選択範囲にバーを引くマクロの改良版

Sub Square_mod()
'
' Square_mod Macro
'
Dim X As Double
Dim Y As Double
Dim X2 As Double
Dim Y2 As Double
Dim L As Double
Dim DW As Double
Dim s As Single
Dim E As Single
Dim i As Integer
If Not TypeName(Selection) = "Range" Then Exit Sub
i = Selection.Cells.Row
DW = Cells(8, 6).Width / Cells(1, 43).Value
Cells(i, 5).Value = Selection.Columns.Count * Cells(1, 43).Value 'duration
s = Int((ActiveCell.Left - Cells(8, 6).Left) / DW) + Cells(8, 6)
E = s + Cells(i, 5) - 1
Cells(i, 3).Value = s 'start date
Cells(i, 4).Value = E 'end date
L = 9#
X = Selection.Left
Y = Selection.Top + L / 2
X2 = Selection.Width
Y2 = Selection.Height - L
ActiveSheet.Shapes.AddShape(msoShapeRectangle, X, Y, X2, Y2). _
Select
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 47
Selection.ShapeRange.Line.Weight = 1.25
With Selection
.Placement = xlMove
.PrintObject = True
End With
End Sub


● 作業開始日(Start Date)・終了日(Finish Date)からバーを引くマクロの改良版

Sub Dulation_mod()
'
' Dulation_mod Macro
'
Dim i As Integer
Dim SSD As Double
Dim SD As Double
Dim ED As Double
Dim Dulation As Double
Dim DW As Double
Dim X1 As Double
Dim Y1 As Double
Dim X2 As Double
Dim Y2 As Double
Dim L As Double
If Not TypeName(Selection) = "Range" Then Exit Sub
i = Selection.Cells.Row
SD = Cells(i, 3) 'To adjust with Start Date Cell
ED = Cells(i, 4) 'To adjust with End Date Cell
Dulation = ED - SD + 1
SSD = Cells(8, 6) 'To adjust with First Date Cell
DW = Cells(8, 6).Width / Cells(1, 43).Value
Range(Cells(i, SD - SSD + 6), Cells(i, Int((ED - SSD) / DW))).Select
L = 9#
X1 = (SD - Cells(8, 6)) * DW + Cells(8, 6).Left
Y1 = Selection.Top + L / 2
X2 = Dulation * DW
Y2 = Selection.Height - L
ActiveSheet.Shapes.AddShape(msoShapeRectangle, X1, Y1, X2, Y2). _
Select
Selection.ShapeRange.Fill.ForeColor.SchemeColor = 47
With Selection
.Placement = xlMove
.PrintObject = True
End With
Cells(i, 5).Value = Dulation 'To adjust with Dulation Column
Cells(i + 1, 3).Activate 'To adjust with Next Start Date Cell
End Sub


詳しくは自分で確認して見るとわかると思いますが、Cells(1, 43) = AQ1 セルに、セル一つ毎の日数を記入する欄を設け、それを変える毎に勝手に日数の計算の修正をしてくれるようになっています。

前回のマクロとの違いをそれぞれよーく見比べてみて下さい。

では又。

0 件のコメント:

コメントを投稿