RangetoHTML file size problem


New Member
Jun 5, 2009
Hi there,

I'm using RangetoHTML to send chunks of a spreadsheet via CDO emails to lots of recipients. The range is fairly small, and contains basic text, however the "RangetoHTML" function (which I am not really keen on since I cut & paste it but have not yet figured out exactly how it works) makes each of my messages 2MB in size, which is pretty poor given the small amount of actual data.

Is there anything I can do to improve the function's efficiency by removing the junk HTML code?


Andrew Poulsom

MrExcel MVP
Jul 21, 2002
Welcome to the Board.

Which version of RangeToHTML are you using? And which version of Excel?
Last edited:


New Member
Jun 5, 2009
Hi there,
I'm on Excel 2003, and the RangetoHTML code I'm using is:

Function RangetoHTML(Rng As Range)
Dim wb As Workbook
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim i As Long
Dim Rng2 As Range
Dim DelCol1 As String
Dim DelCol2 As String
TempFile = Rng.Parent.Parent.Path & "\TmpHTML" & Int(Rnd() * 10) & ".htm"
'Copy the sheet to a new workbook and copy the cells to avoid the
'255 character limit when copying sheets
' Rng.Parent.Cells.Copy ActiveSheet.Cells
Set wb = ActiveWorkbook
Set Rng2 = wb.Sheets(1).Range(Rng.Address)
'Convert to values
Rng2.PasteSpecial xlPasteValues
Application.CutCopyMode = False
'Delete rows below
Rng2.Parent.Rows(Rng2.Rows(Rng2.Rows.Count).Row + 1 & ":65536").Delete
'Delete columns to right
DelCol2 = Chr(64 + Rng2.Parent.Columns(Rng2.Columns _
(Rng2.Columns.Count).Column + 1).Column)
Rng2.Parent.Columns(DelCol2 & ":IV").Delete

'Delete rows above
If Rng2.Rows(1).Row > 1 Then
Rng2.Parent.Rows("2:" & Rng2.Rows(1).Row - 1).Delete
End If
'Delete columns to left
If Rng2.Columns(1).Column > 1 Then
DelCol1 = Chr(64 + Rng2.Parent.Columns(Rng2.Columns(1).Column - 1).Column)
Rng2.Parent.Columns("A:" & DelCol1).Delete
End If
wb.SaveAs TempFile, xlHtml
wb.Close False
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
Set ts = Nothing
Set fso = Nothing
Kill TempFile
End Function

Sorry, not sure how to paste code in with better formatting! Hope you can help!


New Member
Jun 5, 2009
Aha. Excellent.

I had an idea the coding of the function I was using was causing the problem.

The function you suggested does look a lot snappier, and gives me a message size of only 11KB. Just what I was after.

Thanks for your help!

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...