A powerful VBA macro for Microsoft Excel that transforms a selected cell range into a clean, minimalist HTML table. It is specifically designed to prepare complex data structures for analysis by AI models.
When you use Excel's built-in "Save as Web Page" feature, it generates cluttered HTML filled with excessive styling, metadata, and Microsoft-specific tags. This "bloated" code is difficult for AI models to parse efficiently and can lead to inaccurate data interpretation.
Furthermore, this unnecessary code wastes valuable tokens. AI models process information in these small units of data, and more tokens mean higher processing costs and slower analysis.
This macro solves the problem by stripping away all non-essential formatting and attributes. It preserves only the core structure of the table (<table>, <tr>, <td>) and essential structural attributes (rowspan, colspan).
The result is a pure HTML representation of your data, optimized for clarity, machine readability, and cost-effective AI analysis.
- Minimalist Output: Removes all styling, comments, and non-essential metadata.
- Structure Preservation: Retains
rowspanandcolspanattributes to maintain complex table layouts. - Whitespace Cleaning: Normalizes all spacing for a compact and clean output.
- Clipboard Ready: The final HTML code is automatically copied to your clipboard for immediate use.
-
Open the VBA Editor: In Excel, press
Alt + F11. -
Insert a New Module: In the VBA editor, go to Insert > Module.
-
Paste the Code: Copy the code from the
CopySelectionAsCleanHTML.vbafile in this repository and paste it into the new module. -
Run the Macro:
- Return to your Excel sheet and select the range of cells you want to convert.
- Press
Alt + F8to open the Macro dialog. - Select
CopySelectionAsCleanHTMLand click Run.
-
Paste Your Clean HTML: The clean HTML code is now on your clipboard, ready to be pasted into an AI prompt or any other application.
The Source Excel Table:
| 1 | 1 |
|---|---|
| 1 | 1 |
Before: A complex Excel table with redundant code.
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 15">
<link rel=File-List href="temp_excel_20251107162500_files/filelist.xml">
<style id="Book3_2851_Styles">
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
.xl152851
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:black;
font-size:11.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:微软雅黑, sans-serif;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:middle;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:微软雅黑, sans-serif;
mso-font-charset:134;
mso-char-type:none;}
-->
</style>
</head>
<body>
<!--[if !excel]> <![endif]-->
<!--The following information was generated by Microsoft Excel's Publish as Web
Page wizard.-->
<!--If the same item is republished from Excel, all information between the DIV
tags will be replaced.-->
<!----------------------------->
<!--START OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD -->
<!----------------------------->
<div id="Book3_2851" align=center x:publishsource="Excel">
<table border=0 cellpadding=0 cellspacing=0 width=152 style='border-collapse:
collapse;table-layout:fixed;width:114pt'>
<col width=76 span=2 style='width:57pt'>
<tr height=20 style='height:15.0pt'>
<td height=20 class=xl152851 align=right width=76 style='height:15.0pt;
width:57pt'>1</td>
<td class=xl152851 align=right width=76 style='width:57pt'>1</td>
</tr>
<tr height=20 style='height:15.0pt'>
<td height=20 class=xl152851 align=right style='height:15.0pt'>1</td>
<td class=xl152851 align=right>1</td>
</tr>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=76 style='width:57pt'></td>
<td width=76 style='width:57pt'></td>
</tr>
<![endif]>
</table>
</div>
<!----------------------------->
<!--END OF OUTPUT FROM EXCEL PUBLISH AS WEB PAGE WIZARD-->
<!----------------------------->
</body>
</html>After: A clean HTML structure, ready for AI.
<html><body><div><table><col><tr><td>1</td><td>1</td></tr><tr><td>1</td><td>1</td></tr></table></div></body></html>This project is licensed under the MIT License.