你知道怎么绘制甘特图吗?本号以前讲过怎么利用公式来制作甘特图,那是相当的麻烦,做了一个又一个辅助列,今天我来教教大家怎么用VBA来制作甘特图,先来看看效果。 效果展示: 怎么样,一键生成甘特图+美化图表,下面我们来看看代码 代码展示: Sub gantetu() Dim X, Y, Z, F1, F2, F3 Dim sht As Worksheet Set sht = ActiveSheet With sht X = Application.Transpose(.Range('A2:A13').Value) Y = Application.Transpose(.Range('B2:B13').Value) Z = Application.Transpose(.Range('C2:C13').Value) ReDim F1(1 To UBound(Y)) ReDim F2(1 To UBound(Y)) ReDim F3(1 To UBound(Y)) End With For i = 1 To UBound(X) Y(i) = DateValue(Y(i)) * 1 Z(i) = DateValue(Z(i)) * 1 F1(i) = Z(i) - Y(i) F2(i) = Date * 1 F3(i) = i Next i Dim chtobj As ChartObject, cht As Chart Set chtobj = sht.ChartObjects.Add(sht.Range('D1').Left, 0, sht.Range('O1').Left, sht.Range('A20').Top) Set cht = chtobj.Chart Call addser(cht, xlBarStacked, '开始时间', X, Y) Call addser(cht, xlBarStacked, '任务周期', X, F1) Call addser(cht, xlXYScatterSmoothNoMarkers, '当前日期', F2, F3) With cht .Axes(xlValue).MinimumScale = Application.Min(Y) .Axes(xlValue).MaximumScale = Application.Max(Z) .Axes(xlValue).MajorUnit = 10 End With Call 图表美化(cht) End Sub Sub addser(cht As Chart, typ, strname, arrX, arrY) Dim ser As Series Set ser = cht.SeriesCollection.NewSeries With ser .ChartType = typ .Name = strname .XValues = arrX .Values = arrY End With End Sub |
|