【问题标题】:how to convert a column of text with html tags to formatted text in vba in excel如何将带有html标签的一列文本转换为excel中vba中的格式化文本
【发布时间】:2018-03-06 10:30:44
【问题描述】:

我想知道如何使用 VBA 脚本将带有 html 标签的一整列单元格转换为格式化文本(基于这些标签)。

screenshot of spreadsheet

我能够根据之前的列表转换一个单元格:HTML Text with tags to formatted text in an Excel cell

使用以下内容:

子样本() 暗淡作为对象 设置 Ie = CreateObject("InternetExplorer.Application") 与即 .可见=假 .导航“关于:空白” .document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value '更新到包含要转换的 HTML 的单元格 .ExecWB 17, 0 '选择浏览器中的所有内容 .ExecWB 12, 2 '复制它们 ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("B1") '更新到要粘贴转换后的 HTML 的单元格 。放弃 结束于 结束子

但这只会转换列中的第一个单元格。 (在上面的示例中,我手动输入 A2 和 B2 来执行第二个单元格)。如果这是一个幼稚的问题,我很抱歉,但我是 VBA 的新手。我尝试过使用循环和范围,但没有成功。

【问题讨论】:

    标签: html excel vba


    【解决方案1】:

    请检查:

    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
    

    【讨论】:

      【解决方案2】:

      您的代码仅适用于第一行,因为您只获取和设置第一行:

      '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
      

      【讨论】:

      • 你说得对,谢谢,我忘了设置 IE = nothing 在每次迭代结束时杀死 IE 进程,现在可以了
      猜你喜欢
      • 1970-01-01
      • 2020-08-29
      • 2017-06-13
      • 2019-02-20
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      相关资源
      最近更新 更多