Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Gantt Chart Excel 2013 VBA: Step-by-Step Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can build a working Gantt chart in Excel 2013 using VBA, but the fastest reliable route is to let VBA draw the bars on top of a normal stacked bar chart after you set up a small task table. Excel 2013 has no built-in Gantt chart type, so the practical answer is: create a table with Task, Start Date, and Duration, insert a stacked bar chart, then run a short macro that formats the series, reverses the category axis, and sets the date scale. This guide walks through that process, including the exact VBA you need and the mistakes that make the bars disappear or land on the wrong dates.

What You'll Need

  • Excel 2013 on Windows (the VBA editor and chart object model described here are the desktop version; Excel for the web does not run VBA).
  • A workbook saved as .xlsm (macro-enabled) if you want to keep the macro.
  • Macro security set to allow macros, or the workbook opened from a trusted location.
  • A task list with at least three columns: task name, start date, and duration in days.

Menu labels in Excel 2013 can differ slightly by edition and region, so if a button name does not match exactly, look for the closest equivalent on the same tab.

Step-by-Step

  1. Set up the data table. In cells A1:C1, type Task, Start, Duration. Enter your tasks below. Format column B as Date (for example, 01/06/2025) and column C as a whole number of days. Do not leave blank rows inside the table.
  2. Add a helper column for the invisible offset. In D1 type Offset. In D2 enter a formula that converts the start date into a number the chart can plot, for example =B2. Format D as a number, not a date. This column becomes the invisible first segment of each bar.
  3. Insert a stacked bar chart. Select A1:D[last row], go to Insert > Charts > Stacked Bar (the horizontal one). A chart appears with two series: Offset and Duration.
  4. Open the VBA editor. Press Alt+F11. In the Project pane, right-click your workbook, choose Insert > Module. Paste the macro below into the code window.
  5. Run the macro. Place the cursor inside the FormatGantt subroutine and press F5, or go back to Excel and use Developer > Macros. The macro hides the Offset series, reverses the task order so the first task sits at the top, and sets the horizontal axis to a date scale.
  6. Check the axis. Right-click the horizontal axis, choose Format Axis, and confirm the Minimum and Maximum are set to your project start and end dates. The macro sets sensible defaults, but you may need to widen the range if tasks fall outside it.
  7. Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx will strip the macro on save.

The macro to paste is deliberately short so you can read and adjust it:

Sub FormatGantt()
    Dim cht As Chart
    Dim s As Series
    Set cht = ActiveSheet.ChartObjects(1).Chart

    ' Hide the offset series so only the duration bars show
    cht.SeriesCollection(1).Format.Fill.Visible = msoFalse
    cht.SeriesCollection(1).Format.Line.Visible = msoFalse

    ' Reverse the category order so task 1 is at the top
    cht.Axes(xlCategory).ReversePlotOrder = True

    ' Set the value axis to a date scale
    With cht.Axes(xlValue)
        .MinimumScale = Range("B2").Value
        .MaximumScale = Range("B2").Value + Application.WorksheetFunction.Sum(Range("C2:C" & Cells(Rows.Count, "C").End(xlUp).Row))
        .MajorUnit = 7
        .TickLabels.NumberFormat = "dd-mmm"
    End With
End Sub

Adjust the MajorUnit value to change gridline spacing: 1 for daily, 7 for weekly, 14 for fortnightly. If your tasks start at different dates, the minimum should be the earliest start date rather than B2; replace Range("B2").Value with Application.WorksheetFunction.Min(Range("B2:B" & lastRow)) where lastRow is the last data row.

Common Mistakes

  • Leaving the Offset series visible. The chart looks like solid bars starting at zero. The macro hides series 1; if you add series later, the index changes and you must hide the correct one.
  • Formatting dates as text. If column B is text, the axis scale and duration maths break. Re-enter dates as real dates and check the cell alignment (dates right-align by default).
  • Forgetting to reverse the plot order. Tasks appear bottom-up, which reads backwards for most project plans.
  • Saving as .xlsx. The macro is removed without warning. Always use .xlsm.
  • Running the macro with the wrong chart selected. The code targets ChartObjects(1), the first chart on the active sheet. If you have several charts, select the right one or change the index.

Tips

  • Name your chart object (select it, then type a name in the Name Box) and reference it by name in VBA instead of index number; this survives adding or deleting charts.
  • Add a milestone row by setting Duration to 0 and formatting that series as a marker, or add a separate scatter series for milestones.
  • Use conditional formatting on the Duration column to flag tasks longer than a threshold before you chart them.
  • Keep the data table on its own sheet and the chart on a dashboard sheet; the macro still works if you reference the chart by name.
  • Test the macro on a copy of the workbook first, since it changes chart formatting and axis settings.

Troubleshooting

The macro does nothing. Check that macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings) and that the workbook is .xlsm. If the Developer tab is missing, enable it in File > Options > Customize Ribbon.

Bars start at the wrong date. The Offset column is probably formatted as a date or contains text. Set it to General or Number and re-enter the formula.

The chart is empty or shows only one bar. Confirm the source range includes all rows and that Duration values are numbers, not text. Delete the chart and re-insert it after fixing the data.

Axis labels overlap. Increase MajorUnit to 14 or 30, or rotate the labels via Format Axis > Text Options.

Runtime error 438 or 1004. The chart object reference is wrong. Click the chart once to select it, then run the macro again, or change ChartObjects(1) to the correct index.

Frequently Asked Questions

Can I build a Gantt chart in Excel 2013 without VBA?

Yes. A stacked bar chart with a hidden offset series and a reversed category axis gives the same visual result; the macro only automates the formatting steps.

Does this work in Excel for the web or on Mac?

Excel for the web does not run VBA. Excel for Mac supports VBA, but the chart object model and some axis properties differ, so test the macro and adjust property names if needed.

Why does my chart show bars but no task names?

The category axis labels are probably turned off or the task column was not included in the source range. Right-click the axis and check Format Axis > Labels.

Can I update the chart automatically when dates change?

Yes, if the chart's source range covers the full table. Changing a date or duration updates the bars; you may need to re-run the macro to reset the axis minimum and maximum.

How do I add a today line?

Add a second series with a single value equal to today's date, change that series to a line chart type, and format it as a vertical line. This needs a combined chart, which Excel 2013 supports through Change Chart Type > Combo.