首页 > 解决方案 > “反向”在 Excel 中运行 VBA 脚本

问题描述

在这里的第一个计时器VBA:)

我使用脚本在 excel 中将 html 标签显示为 html(而不是纯文本)。脚本效果很好。但是我想知道是否可以运行reverse此脚本的“”版本以将数据显示为显示 html 标记的纯文本。如果有可能,我会怎么做?任何帮助都感激不尽!

脚本是:

Sub DisplayHTMLContentProperly()
Dim rng As Range
Dim row As Range
Dim cell As Range
Dim Ie As Object
Set Ie = CreateObject("InternetExplorer.Application")
Ie.Visible = True 'to be tested
Dim DataObj As MSForms.DataObject ' for clipboard
Set DataObj = New MSForms.DataObject

Dim sht As Worksheet
Dim LastRow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")


Set rng = Range("c2:c169")

For Each row In rng.Rows
For Each cell In row.Cells

Application.CutCopyMode = False
DataObj.GetFromClipboard
myString = " "
DataObj.SetText " "

If Not IsEmpty(cell) Then
With Ie
.Visible = True
.Navigate "about:blank"
While .Busy Or .ReadyState < 4: DoEvents: Wend
.Document.body.InnerHTML = cell 'update to the cell that contains HTML you want converted

.ExecWB 17, 0 'Select all contents in browser
.ExecWB 12, 2 'Copy them

'get data from clipboard (due to copy method above) and paste it into the cell
DataObj.GetFromClipboard
myString = DataObj.GetText(1)
'MsgBox myString - to debug
cell = myString

'delete anything from the clipboard
DataObj.Clear
Application.CutCopyMode = False
DataObj.SetText " "

End With
End If
'Do Something
Set HTML = Nothing
Application.Wait (Now + 0.000000011574 * 1200)

Next cell
Next row

Ie.Quit
Set Ie = Nothing
Application.CutCopyMode = False

MsgBox "I am now done with proper formatting of HTML column."

'move to see just summary page
Sheets("Summary").Activate

End Sub

标签: excelvba

解决方案


不,你不能,你必须编写第二个程序来做相反的事情。相反的过程将是一个完全不同的逻辑。

这不可能是自动的,因为有些功能可以反转,有些则不能。

例如,想象一个对结果Sum求和的函数,但这个结果无法恢复,因为关于它之前的信息不再存在于那个中。也可以是1+2+361+2+3664+2


推荐阅读