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.
Hope this upgraded code will help everyone who's looking for it.
#Tags: #RangetoHTML, #Upgraded, #ExportRangeToE mailWithoutLosi ngFormat
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.
Code:
Function RangetoHTML(rng As Range, Optional TypeCopy As Integer) 'add type copy 0 for normal way, 1 for pivot formated table
'Original By Ron de Bruin. ------ Upgrade by Hv Summer (maihoang.viet@suntorypepsico.vn)
Dim fso As Object
Dim ts As Object
Dim TempFile As String, i As Integer, g As Integer, h As Integer, c As Integer, d As Integer
Dim TempWB As Workbook
Dim FormatRange As Range, FindRange As Variant, ResultRange As Range
Set FormatRange = rng
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Updated this below part (1).---------------------------------------------------------------------------------------------------------------------------------------
'Copy the range and create a new workbook to past the data in
FindRange = rng.value
For i = LBound(FindRange, 1) To UBound(FindRange, 1)
If IsEmpty(FindRange(i, 1)) Then
g = g + 1
Else
g = 0
End If
If g = 100 Then GoTo NextStep1::
Next i
NextStep1::
c = i - g - 1
For i = LBound(FindRange, 1) To UBound(FindRange, 1)
If IsEmpty(FindRange(i, UBound(FindRange, 2))) Then
h = h + 1
Else
h = 0
End If
If h = 100 Then GoTo NextStep2::
Next i
NextStep2::
d = i - h - 1
If d > c Then c = d
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))
ResultRange.SpecialCells(xlCellTypeVisible).Copy
'-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
'Updated this below part (2).----------------------------------------------------------------------------------------------------------------------------------
If TypeCopy = 0 Or IsMissing(TypeCopy) Then
.Cells(1).PasteSpecial xlPasteValues, , True, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
ElseIf TypeCopy = 1 Then
.Cells(1).PasteSpecial xlPasteAllUsingSourceTheme, , True, False
FormatRange.Resize(2, ResultRange.Columns.Count).Copy
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Range("A2:" & Cells(2, rng.Columns.Count + rng.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)).Copy
.Range("A2:" & Cells(Range("A2").CurrentRegion.Rows.Count, rng.Columns.Count + rng.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)).PasteSpecial xlPasteFormats, , False, False
End If
'-----------------------------------------------------------------------------------------------------------------------------------------------------------
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
#Tags: #RangetoHTML, #Upgraded, #ExportRangeToE mailWithoutLosi ngFormat