如何将带html标签的文本列转换为Excel中的vba格式文本

我想知道如何使用VBA脚本来隐藏整个列的单元格与HTML标签格式文本(基于这些标签)。

电子表格的屏幕截图

我能够根据以前的清单转换ONE单元格: 带有标签的HTML文本在Excel单元格中格式化文本

使用以下内容:


子样本()
    昏暗的Ie作为对象
    设置Ie = CreateObject(“InternetExplorer.Application”)
    也就是说
         .Visible = False
        导航“关于:空白”
         .document.body.InnerHTML =表(“Sheet1”)。Range(“A1”)。Value
              '更新到包含要转换的HTML的单元格
         .ExecWB 17,0
              '在浏览器中select所有内容
         .ExecWB 12,2
              '复制它们
         ActiveSheet.Paste目的地:=表格(“Sheet1”)。范围(“B1”)
              '更新到你想要粘贴转换HTML的单元格
         。放弃
    结束
结束小组

但是这只能转换列中的第一个单元格。 (在上面的例子中,我手动键入A2和B2做第二个单元格)。 对不起,如果这是一个天真的问题,但我是新来的VBA。 我试过使用循环和范围玩,但没有成功。

您的代码仅用于第一行,因为您正在获取并仅设置第一行:

'get the A1 cell value .document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value 'set the B1 cell value ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("B1") 

要将所有行的代码应用到循环中,必须执行它。

所以你的代码变成:

 Sub Sample() Dim Ie As Object 'get the last row filled lastRow = Sheets("Sheet1").Range("A" & Sheets("Sheet1").Rows.Count).End(xlUp).Row 'loop to apply the code for all the lines filled For Row = 1 To lastRow Set Ie = CreateObject("InternetExplorer.Application") With Ie .Visible = False .Navigate "about:blank" .document.body.InnerHTML = Sheets("Sheet1").Range("A" & Row).Value 'update to the cell that contains HTML you want converted .ExecWB 17, 0 'Select all contents in browser .ExecWB 12, 2 'Copy them ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("B" & Row) 'update to cell you want converted HTML pasted in .Quit End With Set Ie = Nothing Next End Sub 

请检查:

 Option Explicit Sub Sample() Dim Ie As Object Dim i As Long, lastrow As Long lastrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row On Error Resume Next For i = 1 To lastrow Set Ie = CreateObject("InternetExplorer.Application") With Ie .document.body.InnerHTML.Reset .Visible = False .Navigate "about:blank" .document.body.InnerHTML = Sheets("Sheet1").Cells(i, "A").Value 'update to the cell that contains HTML you want converted .ExecWB 17, 0 'Select all contents in browser .ExecWB 12, 2 'Copy them Sheets("Sheet1").Paste Destination:=Sheets("Sheet1").Cells(i, "B") 'update to cell you want converted HTML pasted in .Quit End With Next End Sub