且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Excel宏将HTML实体转换为文本

更新时间:2023-02-16 21:18:06

创建工作簿的备份.

通过按 Alt + F11 打开VBA编辑器.

Open the VBA editor by pressing Alt+F11.

在您正在使用的工作簿下方的树视图中双击此工作簿".

Double-click "This Workbook" in the treeview at left under the workbook that you are working with.

复制并粘贴以下内容:

Sub UnescapeCharacters()

    ' set this to match your case
    sheetname = "Sheet1"

    Dim sheet As Worksheet
    Set sheet = Me.Worksheets(sheetname)

    For Row = 1 To sheet.UsedRange.Rows.Count
        For Column = 1 To sheet.UsedRange.Columns.Count
            Dim cell As Range
            Set cell = sheet.Cells(Row, Column)

            ' define all your replacements here
            ReplaceCharacter cell, """, """" 'quadruple quotes required
            ReplaceCharacter cell, ",", ","
        Next Column
    Next Row

End Sub

Sub ReplaceCharacter(ByRef cell As Range, ByVal find As String, ByVal replacement As String)

    Dim result As String
    cell.Value = replace(cell.Text, find, replacement, 1, -1)

End Sub

这只会遍历指定工作表中的每个单元格并替换您定义的所有内容.提供的代码替换了您提到的两个字符代码.

This just iterates over every cell in the specified worksheet and replaces everything you define. The provided code replaces the two character codes you mentioned.

您可以将其作为宏运行,也可以将插入符号放在"UnescapeCharacters"子例程中,然后按 F5 .

You can run it as a macro, or just place the caret in the "UnescapeCharacters" subroutine and hit F5.