Hi all,
I am a newbie to VB, but have used Excel quite a bit. I have created a Gantt chart manually using start and duration data, but that took a while, so I borrowed some VB code (from http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q213447) to automate it a little... but now I will have to copy the required cells to another sheet, create the chart using the macro for each group of data. I would like to have a sheet dedicated to producing a chart initiated from a selection in the combo box.
I know exactly what I want, but don't know where to start... the process would be:
1. Sheet A would be prepopulated similar to the following data:
2. Sheet B would have a combo box that would contain the date cell text(e.g. 'Date: 20/12/2004', 'Date: 21/12/2004').
3. The user would select the date required from the combo box, the macro would then determine the two series ranges for the date (Start and Duration) and create a chart (based on the macro code below) which would be placed inside Sheet B.
Note: the X-axis would have to always use the static 'Name' column data.
I hope I haven't confused you. Any help would be appreciated.
Cheers,
Phil
Macro Code:
I am a newbie to VB, but have used Excel quite a bit. I have created a Gantt chart manually using start and duration data, but that took a while, so I borrowed some VB code (from http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q213447) to automate it a little... but now I will have to copy the required cells to another sheet, create the chart using the macro for each group of data. I would like to have a sheet dedicated to producing a chart initiated from a selection in the combo box.
I know exactly what I want, but don't know where to start... the process would be:
1. Sheet A would be prepopulated similar to the following data:
Code:
-------------------------------------------------------------
| Date: 20/12/2004 | Date: 21/12/2004 |...
-------------------------------------------------------------
Name | Start | End | Duration | Start | End | Duration |...
-------------------------------------------------------------
proc1 | 02:30 | 02:45 | 00:15 | 02:10 | 02:45 | 00:35 |...
proc2 | 03:10 | 03:45 | 00:35 | 03:10 | 03:45 | 00:35 |...
proc3 | 04:00 | 04:50 | 00:50 | 04:10 | 04:20 | 00:10 |...
...
3. The user would select the date required from the combo box, the macro would then determine the two series ranges for the date (Start and Duration) and create a chart (based on the macro code below) which would be placed inside Sheet B.
Note: the X-axis would have to always use the static 'Name' column data.
I hope I haven't confused you. Any help would be appreciated.
Cheers,
Phil
Macro Code:
Code:
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:="process", ValueTitle:="time", _
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
With .SeriesCollection(2)
With .Border
.Weight = xlThin
End With
.InvertIfNegative = False
.Interior.ColorIndex = 5
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