Smart Systems for Home

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

Excel VBA Range to HTML: Step-by-Step Guide

· 5 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 convert an Excel range to HTML with a short VBA macro that loops through the cells and writes the values and formatting into an HTML table. The simplest working approach is to build a string that starts with a <table> tag, add a <tr> for each row and a <td> for each cell, then save or email the result. This guide shows exactly how to do that, plus how to keep basic formatting and avoid the common traps.

What You'll Need

  • Excel for Windows or Mac with macros enabled (the Developer tab visible, or permission to open the VBA editor with Alt+F11 / Option+F11).
  • A workbook saved as .xlsm if you want to keep the macro.
  • A range of cells you want to export — ideally with a clear header row.
  • Basic familiarity with the VBA editor: inserting a module and pressing F5 to run code.

Menu labels in Excel can differ slightly by version, platform and region, but the VBA editor itself is consistent across Windows and Mac for the steps below.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 on Windows or Option+F11 on Mac. In the Project pane, right-click your workbook, choose Insert > Module. A blank code window appears.
  2. Paste a basic conversion macro. The core idea is to loop over rows and columns and concatenate HTML. A minimal version looks like this:
    Sub RangeToHTML()
        Dim rng As Range, cell As Range, row As Range
        Dim html As String
        Set rng = Selection
        html = "<table border='1'>"
        For Each row In rng.Rows
            html = html & "<tr>"
            For Each cell In row.Cells
                html = html & "<td>" & cell.Text & "</td>"
            Next cell
            html = html & "</tr>"
        Next row
        html = html & "</table>"
        ' Output: write to a file or a cell
        Dim f As Integer
        f = FreeFile
        Open ThisWorkbook.Path & "\range.html" For Output As #f
        Print #f, html
        Close #f
    End Sub
    This writes a plain HTML table to a file next to the workbook. Select the range first, then run the macro.
  3. Escape special characters. Cell text containing &, < or > will break the HTML. Replace them before writing: cell.Text = Replace(cell.Text, "&", "&amp;") and similarly for < and >. Do this inside the loop.
  4. Add basic formatting (optional). To keep bold headers or background colours, read cell.Font.Bold and cell.Interior.Color and emit inline styles or a <b> tag. Keep it simple: bold and italic are the most reliable.
  5. Write the output where you need it. The example saves to a file. You can also assign the HTML string to a cell (then copy it), or use it in an email via Outlook automation. For email, set the .HTMLBody property of a MailItem to the string.
  6. Run and test. Select a small range (say A1:C3) and press F5. Open the generated file in a browser to confirm the table looks right before scaling up.

Common Mistakes

  • Forgetting to escape &, < and > — the table renders broken or blank.
  • Using cell.Value instead of cell.Text — dates and formatted numbers come out as serial numbers or lose their display format.
  • Looping over the wrong range — using Selection when nothing is selected, or including blank rows that produce empty <tr> tags.
  • Not closing the file handle — the HTML file stays locked and cannot be opened until Excel is closed.
  • Assuming colours survive — Excel colours are long integers; converting them to hex for CSS needs a helper function, so many simple macros drop colour entirely.

Tips

  • Build the HTML in a String and write once at the end; writing line by line to a file is slower for large ranges.
  • Use UsedRange or a named range instead of hard-coding addresses, so the macro keeps working when data grows.
  • For big ranges, turn off screen updating with Application.ScreenUpdating = False and restore it at the end.
  • If you need a full HTML document (with <html> and <body> tags), wrap the table string in those tags before saving.
  • Keep a copy of the original workbook before running any macro that writes files, in case the output path is wrong.

Troubleshooting

The macro does nothing. Check that a range is selected and that macros are enabled. If the Developer tab is missing, enable it in Excel options; the exact wording varies by version.

The HTML file is empty or missing. Confirm ThisWorkbook.Path is not blank — an unsaved workbook has no path. Save the workbook first, or write to a fixed folder such as Environ("TEMP").

The table looks wrong in the browser. View the page source and look for unescaped characters. Also check that every <tr> has the same number of <td> cells; merged cells in Excel can produce uneven rows.

Special characters show as question marks. The file is being written in a non-UTF-8 encoding. For most Western text this is fine; for other scripts, consider writing with a UTF-8 aware method or using an HTML meta charset tag.

Email body is blank. If using Outlook automation, ensure the string is assigned to .HTMLBody, not .Body, and that the mail item is displayed or sent after assignment.

Frequently Asked Questions

Can I convert a range to HTML without VBA?

Yes — copy the range, paste it into a word processor or HTML editor, and save as HTML. VBA is only needed when you want to automate or repeat the export.

Does the macro keep cell colours and fonts?

Not by default. The basic loop above keeps text only. Keeping colours requires converting Excel colour values to CSS hex, which is extra code and not always reliable across themes.

Can I email the HTML table directly from Excel?

Yes, with Outlook installed you can create a MailItem and set its .HTMLBody to the generated string. This works on desktop Outlook; webmail has no direct VBA hook.

Why does my exported table show numbers instead of dates?

Because .Value returns the underlying serial number. Use .Text to get the displayed string, or format the value with Format() before writing.

Is there a size limit for the HTML string?

VBA strings can hold very large text, but extremely large ranges (tens of thousands of rows) will be slow. For big exports, write in chunks or consider a different tool.