473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

RangetoHTML - Upgraded a function using to export table to HTMLbody Excel vba.

215 128KB
I took this function from http://www.rondebruin.nl/win/s1/outlook/bmail2.htm
It work verywell until I select range that no formating from filtered pivot table (to ensure when I change filter, every data still copied), the table pasted become no formating.

so I decided to modify/upgrade the code to suit new demand, when you want to copy filtered pivot table w/out losing format.

added typecopy (0 is normal way, and 1 for special Filtered Pivottable)
When use this function, you can ommit typecopy (default value = 0) or input number 0 or 1 into it.
Expand|Select|Wrap|Line Numbers
  1. Function RangetoHTML(rng As Range, Optional TypeCopy As Integer) 'add type copy 0 for normal way, 1 for pivot formated table
  2. 'Original By Ron de Bruin. ------ Upgrade by Hv Summer (maihoang.viet@suntorypepsico.vn)
  3.     Dim fso As Object
  4.     Dim ts As Object
  5.     Dim TempFile As String, i As Integer, g As Integer, h As Integer, c As Integer, d As Integer
  6.     Dim TempWB As Workbook
  7.     Dim FormatRange As Range, FindRange As Variant, ResultRange As Range
  8.     Set FormatRange = rng
  9.     TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
  10. 'Updated this below part (1).---------------------------------------------------------------------------------------------------------------------------------------
  11.     'Copy the range and create a new workbook to past the data in
  12.     FindRange = rng.value
  13.     For i = LBound(FindRange, 1) To UBound(FindRange, 1)
  14.                 If IsEmpty(FindRange(i, 1)) Then
  15.                     g = g + 1
  16.                 Else
  17.                     g = 0
  18.                 End If
  19.                 If g = 100 Then GoTo NextStep1::
  20.     Next i
  21. NextStep1::
  22. c = i - g - 1
  23.     For i = LBound(FindRange, 1) To UBound(FindRange, 1)
  24.                 If IsEmpty(FindRange(i, UBound(FindRange, 2))) Then
  25.                     h = h + 1
  26.                 Else
  27.                     h = 0
  28.                 End If
  29.                 If h = 100 Then GoTo NextStep2::
  30.     Next i
  31. NextStep2::
  32. d = i - h - 1
  33. If d > c Then c = d
  34. Set ResultRange = rng.Parent.Range(Cells(rng.Row, rng.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False) & ":" & Cells(c + rng.Row - 1, rng.Columns.Count + rng.Column - 1).Address(RowAbsolute:=False, ColumnAbsolute:=False))
  35.  
  36. ResultRange.SpecialCells(xlCellTypeVisible).Copy
  37. '-----------------------------------------------------------------------------------------------------------------------------------------------------------------
  38.     Set TempWB = Workbooks.Add(1)
  39.     With TempWB.Sheets(1)
  40.         .Cells(1).PasteSpecial Paste:=8
  41. 'Updated this below part (2).----------------------------------------------------------------------------------------------------------------------------------
  42.         If TypeCopy = 0 Or IsMissing(TypeCopy) Then
  43.             .Cells(1).PasteSpecial xlPasteValues, , True, False
  44.             .Cells(1).PasteSpecial xlPasteFormats, , False, False
  45.         ElseIf TypeCopy = 1 Then
  46.             .Cells(1).PasteSpecial xlPasteAllUsingSourceTheme, , True, False
  47.         FormatRange.Resize(2, ResultRange.Columns.Count).Copy
  48.         .Cells(1).PasteSpecial xlPasteFormats, , False, False
  49.         .Range("A2:" & Cells(2, rng.Columns.Count + rng.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)).Copy
  50.         .Range("A2:" & Cells(Range("A2").CurrentRegion.Rows.Count, rng.Columns.Count + rng.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)).PasteSpecial xlPasteFormats, , False, False
  51.         End If
  52. '-----------------------------------------------------------------------------------------------------------------------------------------------------------
  53.         .Cells(1).Select
  54.         Application.CutCopyMode = False
  55.         On Error Resume Next
  56.         .DrawingObjects.Visible = True
  57.         .DrawingObjects.Delete
  58.         On Error GoTo 0
  59.     End With
  60.  
  61.     'Publish the sheet to a htm file
  62.     With TempWB.PublishObjects.Add( _
  63.          SourceType:=xlSourceRange, _
  64.          Filename:=TempFile, _
  65.          Sheet:=TempWB.Sheets(1).Name, _
  66.          Source:=TempWB.Sheets(1).UsedRange.Address, _
  67.          HtmlType:=xlHtmlStatic)
  68.         .Publish (True)
  69.     End With
  70.  
  71.     'Read all data from the htm file into RangetoHTML
  72.     Set fso = CreateObject("Scripting.FileSystemObject")
  73.     Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
  74.     RangetoHTML = ts.ReadAll
  75.     ts.Close
  76.     RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
  77.                           "align=left x:publishsource=")
  78.  
  79.     'Close TempWB
  80.     TempWB.Close savechanges:=False
  81.  
  82.     'Delete the htm file we used in this function
  83.     Kill TempFile
  84.  
  85.     Set ts = Nothing
  86.     Set fso = Nothing
  87.     Set TempWB = Nothing
  88. End Function
  89.  
Hope this upgraded code will help everyone who's looking for it.

#Tags: #RangetoHTML, #Upgraded, #ExportRangeToEmailWithoutLosingFormat
Oct 28 '15 #1
0 8668

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Keon | last post by:
Hoi, I'm using a database with alot of records in 1 table (more than 3000). If i want to export this table to excel i only get it till record 2385. Do someone know how i can solve this...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
6
by: Sam Johnson | last post by:
HI I tried to send the following SQL string to an open databse, to export a table into excel format: g.Connection = conn 'valid OleDBConnection and Command objects g.CommandText = "SELECT *...
2
by: amrhi | last post by:
Hello Guys,....! Can you help me how to export my table to excell using php code ..? thanks alot
5
by: =?Utf-8?B?c2NobWlkdGU=?= | last post by:
Hi How can I Export an HTML Table to excel? My goal is a button, and when the user clicks this button a popup appears asking the user to 'open' or 'save' the generated Excel file. Actually...
2
by: Mike Wilson | last post by:
Dear all, I'm sure someone has already done this, so as not to wish to re-invent the wheel - and Google not turning up anything I can use, does anyone have a suitable function into which I can...
1
by: monadel | last post by:
Hi, I am a newbie here for exporting about exporting things into excel. I have a form displaying information that user can add/update which is stored into a table. What I want is a button on the...
19
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.