猿问

使用 VBA/Python 在 Excel 中格式化和修改字符串

我正在尝试编写一个 VBA 脚本,它通过一列单元格和一个单元格,在 html<u></u>标签和两个标签之间的文本下划线,然后从文本中删除这些标签。单元格内部可能有多个标签,它们旁边有其他文本,或者根本没有标签。


到目前为止,我已经能够让脚本在标签之间加下划线,但是当我尝试删除它们时,没有任何效果(有时什么都没有改变,有时标签带有下划线等)。为简洁起见,我省略了输入/输出示例,并希望我的代码存在明显的问题,但可应要求提供。


尝试使用 VBA 解决这个问题最初源于我无法在 Python 中执行此操作,因为对象模型仅与单元格一样低,而不是单元格的内容。任何使用 Python 执行此操作的解决方案也将不胜感激!


非常感谢你的帮助!如果还有什么我能做的来帮助大家,请告诉我!


Sub PleaseUnderline()

'Holds the content between the tags

Dim s As String

'Holds the row number of the active cell

Dim a As Integer

'Holds the location of the beginning of the open tag

Dim b As Integer

'Holds the location of the beginning of the close tag

Dim e As Integer

Dim holder As String

    'Select the last cell in column A and make it the active cell

    Range("A" & ActiveCell.SpecialCells(xlLastCell).Row).Select

    For a = ActiveCell.Row To 1 Step -1

        Range("A" & a).Select

        holder = Range("A" & a).Value

        s = ""

        b = 1

        e = 1

        Do

            b = InStr(b, ActiveCell, "<u>")

            If b = 0 Then Exit Do

            e = b + 1

            e = InStr(e, ActiveCell, "</u>")

            If e = 0 Then

                Exit Do

            Else

                s = Mid(ActiveCell, b + 3, e - b - 3)

            End If

            holder = Replace(holder, "<u>", "", 1, 1)

            holder = Replace(holder, "</u>", "", 1, 1)

            Worksheets("Sheet").Range("A" & a).Value = holder

            ActiveCell.Characters(b, Len(s)).Font.Underline = True

            b = e + 1

        Loop

    Next a

End Sub


阿晨1998
浏览 230回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答