Sie sind auf Seite 1von 3

XL: Visual Basic Module to Create a Gantt Chart

Article ID: 213447 - View products that this article applies to.
This article was previously published under Q213447
Expand all | Collapse all
On This Page
SUMMARY
MORE INFORMATION
o Using the Macro
Properties
Give Feedback
<style>.tocTitle, #tocDiv{display: none;}</style>
SUMMARY
In Microsoft Excel, you can create Gantt charts that show the task status for project planning and control. This article describes how to programmatically
generate this type of chart using a Microsoft Visual Basic for Applications procedure.
Back to the top | Give Feedback
MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied
warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being
demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular
procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more
information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104


Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice


For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web
site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
To create a Visual Basic for Applications macro in Excel that you can use to create a Gantt chart, follow these steps:
1. Start Excel.
2. Press ALT+F11 to start the Visual Basic Editor.
3. On the Insert menu, click Module.
4. Type or paste the following code in the Visual Basic module sheet:
5. Option Explicit
6.
7. Sub Gantt_Chart()
8. 'Define the variables.
9. Dim rge As String
10. Dim ValueAxisMinValue As Date
11. Dim shtname As String
12. Dim Title As String, aChart As Chart
13. 'Store the location of the data as a string.
14. rge = Selection.Address()
15. 'Store the start date for the chart.
16. ValueAxisMinValue = Selection.Cells(2, 2).Value
17. 'Ask user for the Chart title.
18. Title = InputBox("Please enter the title")
19. 'Store the sheet name.
20. shtname = ActiveSheet.Name
21. 'Turn off screen updating.
22. Application.ScreenUpdating = False
23. 'Create a chart located on a chart sheet.
24. Set aChart = Charts.Add
25. With aChart
26. .ChartWizard Source:=Sheets(shtname).Range(rge), _
27. Gallery:=xlBar, Format:=3, PlotBy:=xlColumns, _
28. CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _
29. Title:=Title, CategoryTitle:="", ValueTitle:="", _
30. ExtraTitle:=""
31. 'Remove the legend.
32. .Legend.Delete
33. 'Create and format the series.
34. With .SeriesCollection(1)
35. With .Border
36. .Weight = xlThin
37. .LineStyle = xlNone
38. End With
39. .InvertIfNegative = False
40. .Interior.ColorIndex = xlNone
41. End With
42. 'Modify the category (x) axis.
43. With .Axes(xlCategory)
44. .ReversePlotOrder = True
45. .TickLabelSpacing = 1
46. .TickMarkSpacing = 1
47. .AxisBetweenCategories = True
48. End With
49. 'Modify the value (y) axis.
50. With .Axes(xlValue)
51. .MinimumScale = ValueAxisMinValue
52. .MaximumScaleIsAuto = True
53. .MinorUnitIsAuto = True
54. .MajorUnitIsAuto = True
55. .Crosses = xlAutomatic
56. .ReversePlotOrder = False
57. .ScaleType = False
58. .HasMajorGridlines = True
59. .HasMinorGridlines = False
60. End With
61. End With
62. 'Turn screen updating back on.
63. Application.ScreenUpdating = True
64.
65. End Sub

66. Press ALT+F11 to return to Excel.
Using the Macro
To use the Gantt_chart macro, follow these steps:
1. In a new Excel worksheet, type the following values:
2. A1: B1: START C1: DAYS D1: DAYS
3. A2: TASK B2: DATE C2: COMPLETED D2: REMAINING
4. A3: TASK-1 B3: 1/1/91 C3: 150 D3: 15
5. A4: TASK-2 B4: 5/1/91 C4: 21 D4: 31
6. A5: TASK-3 B5: 7/1/91 C5: 0 D5: 114
7. A6: TASK-4 B6: 10/1/91 C6: 0 D6: 4
8. A7: TASK-5 B7: 10/15/91 C7: 0 D7: 31
9. A8: TASK-6 B8: 11/1/91 C8: 0 D8: 2

10. Select cells A2:D8.
11. On the Tools menu, point to Macro, and then click Macros.
12. Click Gantt_chart, and then click Run.
13. Type the chart title when prompted. The Gantt chart is displayed on a new chart sheet. You may add any additional formatting that you
want.

Das könnte Ihnen auch gefallen