You can create a chart in Excel with VBA by writing a few lines in the Visual Basic Editor that add a chart object or chart sheet and then point it at a data range. At its simplest: press Alt+F11 (Windows) or go to Tools > Macros > Visual Basic Editor (Mac), insert a module, and run a macro that calls Shapes.AddChart2 or ChartObjects.Add. Automating charts pays off when you rebuild the same report every week, when you have dozens of ranges to chart, or when a chart must update on a button click. Labels in the menus can differ slightly by Excel version, device and region, but the VBA object model described here is stable across versions.

What You'll Need

  • Excel for Windows (desktop) or Excel for Mac (desktop). VBA chart creation does not work in Excel for the web.
  • A workbook with at least one small table of data, ideally with a header row and numeric values in adjacent columns.
  • Macros enabled: open the file and, if prompted, click Enable Content. You may also need to save as an .xlsm (macro-enabled) workbook to keep the code.
  • The Developer tab visible (optional). Right-click the ribbon and choose Customize the Ribbon, then tick Developer.

Step-by-Step

  1. Lay out clean source data. Put your category labels in one column (for example A2:A7 for months) and numeric series in the next columns (B1:C7, with headers in row 1). Blank cells, text mixed into number columns, and totals rows inside the range are the three most common reasons a generated chart looks wrong. Decide now exactly which range you want plotted, such as Sheet1!A1:C7.
  2. Open the Visual Basic Editor. Press Alt+F11 on Windows. On Mac, use Tools > Macros > Visual Basic Editor. If nothing happens on Windows, your keyboard layout or an add-in may be intercepting the shortcut; the Developer tab and the Visual Basic button do the same job.
  3. Insert a standard module. In the editor choose Insert > Module. Code placed in ThisWorkbook or a sheet module also runs, but a standard module is the conventional home for reusable chart macros and is easier to find later.
  4. Write the basic chart macro. Type the skeleton below, adjusting sheet and range names to match your workbook:
    Sub MakeChart()
        Dim ws As Worksheet
        Dim rng As Range
        Dim cht As Chart
        Set ws = ThisWorkbook.Worksheets("Sheet1")
        Set rng = ws.Range("A1:C7")
        Set cht = ws.Shapes.AddChart2(240, xlColumnClustered).Chart
        cht.SetSourceData Source:=rng
        cht.HasTitle = True
        cht.ChartTitle.Text = "Monthly Sales"
    End Sub
    AddChart2 creates an embedded chart on the worksheet and returns it, so you can format it immediately. The first argument is a style index; 240 is a common default, and the second argument picks the chart type.
  5. Run the macro. Press F5 with the cursor inside the procedure, or go back to Excel and use Developer > Macros, select MakeChart and click Run. If the code editor highlights a line in yellow with an error dialog, read the message: it usually names a misspelled object or a sheet that does not exist.
  6. Position the chart deliberately. By default the chart lands in the middle of the sheet at a default size. Control it explicitly:
    With cht.Parent
        .Top = ws.Range("E2").Top
        .Left = ws.Range("E2").Left
        .Width = 420
        .Height = 280
    End With
    Anchoring to a cell's Top and Left properties keeps the chart aligned with your layout even if columns are resized slightly.
  7. Change the chart type and series names. Swap xlColumnClustered for xlLine, xlXYScatter, xlPie or xlBarClustered as needed. Then tidy the legend and axis titles with cht.HasLegend = True and cht.Axes(xlValue).HasTitle = True, followed by setting cht.Axes(xlValue).AxisTitle.Text = "Units". Setting titles in code avoids the placeholder text Excel inserts.
  8. Create a chart on its own sheet instead. If the chart should fill a dedicated tab, use Charts.Add rather than an embedded object:
    Dim cs As Chart
    Set cs = ThisWorkbook.Charts.Add
    cs.Name = "SalesChart"
    cs.SetSourceData Source:=ws.Range("A1:C7")
    cs.ChartType = xlLine
    Note the sheet name here also becomes the visible tab label, and it must be unique.
  9. Make the range dynamic. Hard-coded ranges break when rows are added. Replace the fixed range with one sized from the used area:
    Set rng = ws.Range("A1").CurrentRegion
    CurrentRegion expands to the contiguous block around A1, so last month's extra row is included automatically. Alternatively use ws.Cells(ws.Rows.Count, 1).End(xlUp).Row to find the last filled row.
  10. Delete the old chart before rebuilding. Running the macro twice leaves two charts stacked. Either remove existing chart objects first with a loop over ws.ChartObjects and .Delete, or capture the chart reference and reuse it. Deleting a chart object removes only the chart, not your data, but check you are not deleting a chart someone hand-formatted.
  11. Attach it to a button. Insert a rectangle or form button from Insert > Shapes or the Developer tab, right-click it, choose Assign Macro, and pick MakeChart. The report now refreshes with one click instead of a trip to the editor.
  12. Save as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). Saving as .xlsx strips the macro without warning.

Common Mistakes

  • Plotting the header row as data. If your headers are numbers or the range starts below them, series names disappear. Either include row 1 in the range or set series names explicitly.
  • Assuming ActiveSheet is the right sheet. Macros run against whatever sheet is active, so a macro that works today can fail after the user clicks another tab. Always qualify with an explicit worksheet object, as in the examples above.
  • Recreating instead of updating. Calling AddChart2 on every run piles up chart objects and slows the workbook. Update an existing chart's source data instead.
  • Forgetting Mac support differences. Some older chart-related methods and constants behave differently or are unavailable in older Mac builds. Test on the platform your users actually have.
  • Saving to .xlsx. The code vanishes on save. This catches almost everyone once.

Tips

  • Record a macro while you build and format a chart by hand, then read the generated code. It is the fastest reliable way to discover the exact property names for formatting you want, even if the recorded code is verbose.
  • Turn off screen updating while generating many charts: Application.ScreenUpdating = False at the start, and set it back to True at the end, including in error handling.
  • Name chart objects (cht.Parent.Name = "chSales") so later code can find them by name rather than by index, which shifts when charts are added or deleted.
  • Keep one small procedure per job (build, format, delete) so you can reuse and debug pieces independently.

Troubleshooting

Nothing happens when you run the macro. Confirm macros are enabled, that you are in a desktop Excel app rather than the browser version, and that the macro is in a module of the workbook you have open. Check the Immediate window for output if your code produces none.

Run-time error naming a range or sheet. The sheet name in your code does not match the tab exactly, including spaces and capitals. Print the name with a quick Debug.Print ws.Name to confirm.

The chart appears empty or shows a single flat line. The source range is probably text-formatted numbers. Select the column, use Data > Text to Columns and finish, or convert with Value = Value in code. Also check the range does not include a totals row inside the plotted block.

Error when setting the chart title. Set HasTitle = True before assigning ChartTitle.Text; assigning to a title that does not exist triggers an error.

Formatting code seems ignored. Some properties must be set after the source data and chart type are assigned, because changing the type resets formatting. Order your code: create, set data, set type, then format.

If that did not work, step through the procedure line by line with F8 and note where execution stops. That narrows the fault to one statement far faster than guessing. As a last resort, comment out everything after chart creation, confirm the bare chart builds, then reintroduce formatting one block at a time.

Frequently Asked Questions

Can I create a chart in Excel for the web with VBA?

No. VBA macros are not supported in the browser version. Use the desktop apps to run chart macros.

Does the macro work on Mac?

Generally yes for the core chart creation shown here. A few older formatting methods and constants differ, so test on the Mac build in use.

How do I make the chart update when data changes?

Point the chart at a range and it reflects edits in that range automatically. If rows are added, rebuild the range with CurrentRegion or reference an Excel Table, which expands as rows are entered.

Can I create several charts at once?

Yes. Loop over a list of ranges or worksheet names and create a chart inside the loop, adjusting Top and Left by an offset each pass so they do not stack.

Why does my chart show the wrong series names?

The header cells were excluded from the source range, or the range was defined as values only. Include the header row or set each series name explicitly in code.