You can create a bar (horizontal) chart in Excel with a macro by writing a short VBA routine that adds a chart object, assigns the data range, and sets the chart type to xlBarClustered. A macro lets you rebuild the same styled chart on demand, or build one chart per row of a table, without clicking through the ribbon each time. If you just need one chart quickly, the Insert tab is faster; macros earn their keep on repeated, identical charts.

What You'll Need

  • Excel on Windows or Mac desktop. VBA macros do not run in Excel for the web, so you need the desktop app.
  • A worksheet with data arranged in columns: one column of category labels and one or more columns of values, with a header row.
  • Macros enabled in the file, saved as .xlsm. If macros are blocked by your organisation's policy, ask IT rather than working around it.
  • Optional: the Developer tab showing. If it is not there, right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list. Labels can differ slightly by version, device or region.

Step-by-Step

  1. Lay out your data cleanly. Put a header row at the top, categories down column A, and numbers in column B (add C, D and so on for more series). Do not leave blank rows inside the data block, and avoid merged cells in the range, because the chart will read them as gaps or zero values.
  2. Select the whole block including headers, from the top-left header cell to the last number. Note the range address in the Name Box at the left of the formula bar, for example A1:B8. You will paste this into the code.
  3. Open the VBA editor with Alt+F11 on Windows. On a Mac, use Tools > Macro > Visual Basic Editor from the Excel menu.
  4. Insert a module: in the editor, use Insert > Module. A blank code window appears. This module is where your macro lives.
  5. Paste this starting macro into the window:
Sub MakeBarChart()
    Dim ws As Worksheet
    Dim rng As Range
    Dim cht As ChartObject

    Set ws = ActiveSheet
    Set rng = ws.Range("A1:B8")   ' change to your data range

    ' Remove an earlier chart of the same name, if present
    On Error Resume Next
    ws.ChartObjects("MyBarChart").Delete
    On Error GoTo 0

    Set cht = ws.ChartObjects.Add(Left:=300, Top:=50, Width:=400, Height:=250)
    cht.Name = "MyBarChart"
    cht.Chart.SetSourceData Source:=rng
    cht.Chart.ChartType = xlBarClustered
    cht.Chart.HasTitle = True
    cht.Chart.ChartTitle.Text = "Sales by Region"
End Sub
  1. Change the range and title to match your sheet. Edit "A1:B8" and "Sales by Region", then press F5 to run the macro from the editor.
  2. Check the result. A horizontal bar chart appears at the position given by Left and Top. The first column of your range becomes the category labels and later columns become series, because the header row is included.
  3. Run it from the sheet. Go back to Excel and use Developer > Macros, choose MakeBarChart, and click Run. On a Mac, use Tools > Macro > Macros.
  4. Style the chart with code if you want consistency. For example, add cht.Chart.HasLegend = False for a single-series chart, or set cht.Chart.ChartGroups(1).GapWidth = 60 to widen the bars. Each property you set is re-applied on every run.
  5. Make it handle many rows automatically by replacing the fixed range with a dynamic one: Set rng = ws.Range("A1").CurrentRegion. This picks up contiguous data around A1, so adding rows later needs no code change.
  6. Save as .xlsm. Use File > Save As and choose Excel Macro-Enabled Workbook. A plain .xlsx save will strip the macro and you will get a warning when saving.
  7. Optionally add a button. On the Developer tab, insert a Form Control button, draw it on the sheet, and assign the macro to it so anyone can rebuild the chart with one click.

Common Mistakes

  • Including the wrong rows. If your range starts below the headers, Excel treats the first data row as series names. Start at the header cell.
  • Chart type confusion. xlBarClustered draws horizontal bars; xlColumnClustered draws vertical columns. People asking for a bar chart often want columns, so pick deliberately.
  • Stacking charts on top of each other. Running the macro repeatedly without deleting the previous chart leaves a pile of overlapping objects. The delete-by-name lines above prevent that.
  • Hard-coded ranges that rot. A fixed "A1:B8" breaks when data grows. Use CurrentRegion or a named range.
  • Forgetting the .xlsm format, then wondering where the macro went after reopening.
  • Numbers stored as text. Values imported from other systems may be text, and the chart shows empty bars. Check with =ISNUMBER() and convert with Text to Columns or multiplication by 1.
  • Blank cells inside the range, which create gaps or misleading zero bars.

Tips

  • Record a macro while you build one chart by hand (Developer > Record Macro), then read the generated code to learn the exact property names for the styling you want.
  • Keep the chart position relative to the data, or place charts on their own sheet with ChartObjects.Add on a dedicated worksheet, so rows shifting do not hide the chart.
  • Name chart objects meaningfully. It makes the delete-and-recreate pattern reliable when the macro runs many times.
  • Use a loop to build one chart per row or per region, naming each chart with an index, when producing a dashboard.
  • Comment your range references so the next person knows what to edit.

Troubleshooting

The macro does nothing. Check the code is in a standard module, not a sheet module, and that the range address is correct. Press F8 to step through line by line and see where it stops.

"Subscript out of range" or "Method failed". Usually a wrong sheet or chart name. Confirm the active sheet holds the data, and that the chart name you delete matches the one you create.

Bars are empty or the wrong way round. Verify the values are numbers, not text, and that category labels sit to the left of the values. Swapping columns changes which axis carries the labels.

Macros are disabled. A yellow security bar or a blocked-content message means the file needs enabling, or your organisation's policy blocks macros. Enable content only for files you trust and that you created. Never download macro-enabled workbooks from unknown sources.

Data grows and the chart misses rows. Switch to CurrentRegion or expand a defined name, then run the macro again. If that did not work, close and reopen the workbook, confirm the file is .xlsm, and re-run.

Frequently Asked Questions

Can I make a bar chart with a macro in Excel for the web?

No. VBA macros run only in the desktop apps on Windows and Mac. In the browser you can insert a bar chart manually from the Insert tab, but you cannot run or record macros.

What is the difference between xlBarClustered and xlColumnClustered?

xlBarClustered produces horizontal bars with categories stacked down the axis. xlColumnClustered produces vertical columns. Both are clustered, meaning multiple series sit side by side rather than stacked.

Why does my macro create a new chart every time it runs?

Because nothing removes the old one. Include a delete-by-name step before adding the chart, or loop through the sheet's chart objects and delete the ones you own.

Can the macro chart update when the data changes?

Yes. If you point the chart at a range or defined name, edits to those cells redraw the chart automatically. If you copied static values into the chart, it will not update.

Does saving as .xlsx break the macro?

It removes it. Excel warns you when saving macro code to a macro-free format. Choose Excel Macro-Enabled Workbook (.xlsm) to keep the code.

Can I put the chart on its own sheet with VBA?

Yes. Instead of a chart object on the data sheet, add a new worksheet and use its chart objects, or create a chart sheet, so the chart has a full page of its own.