213447

2
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 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. 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 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (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 (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. 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. Select cells A2:D8. 3. On the Tools menu, point to Macro, and then click Macros. 4. Click Gantt_chart, and then click Run. SUMMARY MORE INFORMATION Option Explicit Sub Gantt_Chart() 'Define the variables. Dim rge As String Dim ValueAxisMinValue As Date Dim shtname As String Dim Title As String, aChart As Chart 'Store the location of the data as a string. rge = Selection.Address() 'Store the start date for the chart. ValueAxisMinValue = Selection.Cells(2, 2).Value 'Ask user for the Chart title. Title = InputBox("Please enter the title") 'Store the sheet name. shtname = ActiveSheet.Name 'Turn off screen updating. Application.ScreenUpdating = False 'Create a chart located on a chart sheet. Set aChart = Charts.Add With aChart .ChartWizard Source:=Sheets(shtname).Range(rge), _ Gallery:=xlBar, Format:=3, PlotBy:=xlColumns, _ CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _ Title:=Title, CategoryTitle:="", ValueTitle:="", _ ExtraTitle:="" 'Remove the legend. .Legend.Delete 'Create and format the series. With .SeriesCollection(1) With .Border .Weight = xlThin .LineStyle = xlNone End With .InvertIfNegative = False .Interior.ColorIndex = xlNone End With 'Modify the category (x) axis. With .Axes(xlCategory) .ReversePlotOrder = True .TickLabelSpacing = 1 .TickMarkSpacing = 1 .AxisBetweenCategories = True End With 'Modify the value (y) axis. With .Axes(xlValue) .MinimumScale = ValueAxisMinValue .MaximumScaleIsAuto = True .MinorUnitIsAuto = True .MajorUnitIsAuto = True .Crosses = xlAutomatic .ReversePlotOrder = False .ScaleType = False .HasMajorGridlines = True .HasMinorGridlines = False End With End With 'Turn screen updating back on. Application.ScreenUpdating = True End Sub A1: B1: START C1: DAYS D1: DAYS A2: TASK B2: DATE C2: COMPLETED D2: REMAINING A3: TASK-1 B3: 1/1/91 C3: 150 D3: 15 A4: TASK-2 B4: 5/1/91 C4: 21 D4: 31 A5: TASK-3 B5: 7/1/91 C5: 0 D5: 114 A6: TASK-4 B6: 10/1/91 C6: 0 D6: 4 A7: TASK-5 B7: 10/15/91 C7: 0 D7: 31 A8: TASK-6 B8: 11/1/91 C8: 0 D8: 2 Find it myself Ask the community Get live help Select the product you need help with Windows Internet Explorer Office Surface Xbox Skype Windows Phone More products Page 1 of 2 XL: Visual Basic Module to Create a Gantt Chart 8/16/2013 http://support.microsoft.com/kb/213447

Upload: sunday-paul

Post on 19-Jan-2016

4 views

Category:

Documents


0 download

DESCRIPTION

excel WEB TUTORIAL

TRANSCRIPT

XL: Visual Basic Module to Create a Gantt ChartArticle ID: 213447 - View products that this article applies to.

This article was previously published under Q213447

In Microsoft Excel, you can create Gantt charts that show the task status for project planning and control. This article describes how to programmaticallygenerate this type of chart using a Microsoft Visual Basic for Applications procedure.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the impliedwarranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language beingdemonstrated 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, visitthese Microsoft Web sites:

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

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (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 Website:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) To create a Visual Basic forApplications 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. 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. Select cells A2:D8.3. On the Tools menu, point to Macro, and then click Macros.4. Click Gantt_chart, and then click Run.

SUMMARY

MORE INFORMATION

Option Explicit Sub Gantt_Chart() 'Define the variables. Dim rge As String Dim ValueAxisMinValue As Date Dim shtname AsString Dim Title As String, aChart As Chart 'Store the location of the data as a string. rge = Selection.Address()'Store the start date for the chart. ValueAxisMinValue = Selection.Cells(2, 2).Value 'Ask user for the Chart title.Title = InputBox("Please enter the title") 'Store the sheet name. shtname = ActiveSheet.Name 'Turn off screen updating.Application.ScreenUpdating = False 'Create a chart located on a chart sheet. Set aChart = Charts.Add WithaChart .ChartWizard Source:=Sheets(shtname).Range(rge), _ Gallery:=xlBar, Format:=3, PlotBy:=xlColumns, _CategoryLabels:=1, SeriesLabels:=1, HasLegend:=1, _ Title:=Title, CategoryTitle:="", ValueTitle:="", _ ExtraTitle:=""'Remove the legend. .Legend.Delete 'Create and format the series. With .SeriesCollection(1) With .Border .Weight =xlThin .LineStyle = xlNone End With .InvertIfNegative = False .Interior.ColorIndex = xlNone End With 'Modify thecategory (x) axis. With .Axes(xlCategory) .ReversePlotOrder = True .TickLabelSpacing = 1 .TickMarkSpacing =1 .AxisBetweenCategories = True End With 'Modify the value (y) axis. With .Axes(xlValue) .MinimumScale =ValueAxisMinValue .MaximumScaleIsAuto = True .MinorUnitIsAuto = True .MajorUnitIsAuto = True .Crosses =xlAutomatic .ReversePlotOrder = False .ScaleType = False .HasMajorGridlines = True .HasMinorGridlines = False End WithEnd With 'Turn screen updating back on. Application.ScreenUpdating = True End Sub

A1: B1: START C1: DAYS D1: DAYS A2: TASK B2: DATE C2: COMPLETED D2: REMAINING A3: TASK-1 B3: 1/1/91 C3: 150 D3: 15 A4:TASK-2 B4: 5/1/91 C4: 21 D4: 31 A5: TASK-3 B5: 7/1/91 C5: 0 D5: 114 A6: TASK-4 B6: 10/1/91 C6: 0 D6: 4 A7: TASK-5 B7:10/15/91 C7: 0 D7: 31 A8: TASK-6 B8: 11/1/91 C8: 0 D8: 2

Find it myself

Ask the community

Get live help

Select the product you need help with

Windows InternetExplorer

Office Surface Xbox Skype WindowsPhone

More products

Page1 of 2XL: Visual Basic Module to Create a Gantt Chart

8/16/2013http://support.microsoft.com/kb/213447

Article ID: 213447 - Last Review: January 24, 2007 - Revision: 2.6

APPLIES TOMicrosoft Excel 2000 Standard EditionMicrosoft Excel 2002 Standard EditionMicrosoft Excel 97 Standard Edition

Keywords:  kbhowto KB213447

Back to the top

Properties

Give Feedback

Page2 of 2XL: Visual Basic Module to Create a Gantt Chart

8/16/2013http://support.microsoft.com/kb/213447