我从这个功能
http://www.rondebruin.nl/win/s1/outlook/bmail2.htm直到我从过滤的数据透视表中选择不格式化的范围(以确保在我更改过滤器时,每个数据仍在复制),粘贴的表格都没有格式化之前,它工作得很好。
因此,当您要复制不带丢失格式的过滤后的数据透视表时,我决定修改/升级代码以适应新的需求。
添加了typecopy(0是正常方式,而1是特殊的Filtered Pivottable)
使用此功能时,可以省略typecopy(默认值= 0)或在其中输入数字0或1。
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,#ExportRangeToEmailWithoutLosingFormat