Can Excel interpret a cell as HTML?

社会主义新天地 提交于 2019-11-27 21:10:55

Unfortunately the answer is no.

Excel has two HTML options:

  • Open a HTML file, which will sort of render the HTML, sort of, but won't contain any actual HTML in cells
  • Store HTML in cells, but as unformatted text.

You could, maybe possibly, come up with a macro that lets you enter HTML into a cell, then saves that HTML as a document, opens it up in another instance of Excel, then grabs that formatted HTML and places it in the original document; that way you would have two columns, one with the HTML, and one with the output. It would be very unsightly though. Don't do it :0)

Pasting html data in excel will result in the html being properly displayed in excel. The one issue with this is that carriage returns and tabs will be pasted to the next cell.

Dim objData As New DataObject
objData.SetText(sHTML)
Clipboard.SetDataObject(objData)
objRange.PasteSpecial()

Will fill a cell with properly formated text

BornToCode

This code worked for me on one cell (inspired by @Rick's answer, but with few changes because Clipboard.SetDataObject(objData) caused error and also objRange.PasteSpecial() didn't work):

Private Sub Worksheet_Change2(ByVal Target As Range, ByVal sht As Worksheet)
     Dim objData As DataObject 'Set a reference to MS Forms 2.0'
     Dim sHTML As String
     Dim sSelAdd As String
     Application.EnableEvents = False     
     objData = New DataObject
     sHTML = Target.Text
     objData.SetText sHTML
     objData.PutInClipboard
     sht.PasteSpecial Format:="Unicode Text"
     Application.EnableEvents = True
End Sub

Sub test()
     Dim rng As Range
     Set rng = ActiveSheet.Range("F15") 'cell to change'
     Worksheet_Change2 rng, ActiveSheet 
End Sub

see this post for some more details

I guess it shouldn't be too difficult to tweak it a bit that it would work for entire worksheet and not only one specific cell, you should probably add some if condition to wrap this code in order to prevent errors, see this post for some more info

StackOverflowFan

I found an interesting YouTube video which shows how to create a simple HTML Interpreter (VBA) in Microsoft Excel using the web browser control. Enter your HTML and CSS code into a text box, and the form will convert the HTML into a web preview.

HTML Interpreter in Microsoft Excel 2010/2007 - Write directly to Web Browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!