我有一个Excel 2013工作表,其中包含一些HTML格式的数据(特别是下标和上标)。我正在尝试适当地重新格式化数据。当我遍历每个单元格时,我运行一个宏来格式化HTML并去掉标签。对于第一个单元格,它工作得很好。对于第二个单元格,我收到此错误:“”
有关正在发生的事情的任何想法?
'这个单元格工作正常 - 它将o字符转换为上标并删除标记 p>
10) Using the above graphic, what is the temperature at point A?
A. 2<sup>o</sup> C
B. 4<sup>o</sup> C
C. 5<sup>o</sup> C
D. 7<sup>o</sup> C
'此单元格抛出异常
43) Which is true for all Mesothermal (C) climates?
A. The wamest month is between 10 and 18<sup>o</sup>C
B. The warmest month is greater than 10<sup>o</sup>C and the coldest month is between 0 and 10<sup>o</sup>C
C. The coldest month is below 0<sup>o</sup>C
D. All of the above
这是宏代码:
Sub FormatHTML(inval As Variant)
Dim outval As String
Dim isSup As Boolean
Dim isSub As Boolean
Dim start As Integer
Dim start2 As Integer
isSup = InStr(ActiveCell.Value, "<sup>") > 0
isSub = InStr(ActiveCell.Value, "<sub>") > 0
Do Until isSup = False And isSub = False
If isSup Then
start = InStr(ActiveCell.Value, "<sup>") + 5
With ActiveCell.Characters(start:=start, Length:=1).Font
.Superscript = True
End With
With ActiveCell.Characters(start:=(start - 5), Length:=5)
.Delete
End With
start2 = InStr(ActiveCell.Value, "</sup>")
With ActiveCell.Characters(start:=start2, Length:=6)
.Delete
End With
End If
If isSub Then
start = InStr(ActiveCell.Value, "<sub>") + 5
With ActiveCell.Characters(start:=start, Length:=1).Font
.Subscript = True
End With
With ActiveCell.Characters(start:=(start - 5), Length:=5)
.Delete
End With
start2 = InStr(ActiveCell.Value, "</sub>")
With ActiveCell.Characters(start:=start2, Length:=6)
.Delete
End With
End If
isSup = InStr(ActiveCell.Value, "<sup>") > 0
isSub = InStr(ActiveCell.Value, "<sub>") > 0
Loop
End Sub
谢谢, 格伦
答案 0 :(得分:2)
如果不了解更多信息(例如,您是将其用于其他HTML标记,还是仅用于上标?),则以下内容将删除<sup>o</sup>
并替换为度数符号:
Sub test2()
Dim lastRow As Integer
Dim rng As Range, cel As Range
lastRow = ActiveSheet.UsedRange.Rows.Count
Set rng = Range(Cells(1, 1), Cells(lastRow, 1))
rng.Replace What:="<sup>o</sup>", Replacement:="°", LookAt:=xlPart _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
您也可以对<sub>
执行相同操作,只需复制rng.Replace ...
并使用<sub>
代替<sup>
。这是否有效,或者是否需要删除更多HTML等?
编辑:好的,问题是.Characters.Delete
不能用于超过255个字符的单元格。那么,我想要做的是,如果细胞长度> 255,分成两个单元格,一次处理一个。这仍然需要调整,但我想我已经把我现在拥有的东西:
Sub FormatHTML()
Dim outval As String
Dim isSup As Boolean, isSub As Boolean
Dim start As Integer, start2 As Integer, lastRow As Integer, i As Integer
Dim rng As Range, cel As Range
isSup = InStr(ActiveCell.Value, "<sup>") > 0
isSub = InStr(ActiveCell.Value, "<sub>") > 0
Set cel = ActiveCell
lastRow = ActiveSheet.UsedRange.Rows.Count
Set rng = Range(Cells(1, 1), Cells(lastRow, 2))
For Each cel In rng
If Len(cel) > 255 Then
cel.Offset(0, 1).Value = Right(cel.Value, Len(cel.Value) - 255)
cel.Value = Left(cel.Value, 255)
isSup = InStr(cel.Value, "<sup>") > 0
isSub = InStr(cel.Value, "<sub>") > 0
End If
Do Until isSup = False And isSub = False
If isSup Then
start = InStr(cel.Value, "<sup>") + 5
With cel.Characters(start:=start, Length:=1).Font
.Superscript = True
End With
With cel.Characters(start:=(start - 5), Length:=5)
.Delete
End With
start2 = InStr(cel.Value, "</sup>")
With cel.Characters(start:=start2, Length:=6)
.Delete
End With
End If
If isSub Then
start = InStr(cel.Value, "<sub>") + 5
With cel.Characters(start:=start, Length:=1).Font
.Subscript = True
End With
With cel.Characters(start:=(start - 5), Length:=5)
.Delete
End With
start2 = InStr(cel.Value, "</sub>")
With cel.Characters(start:=start2, Length:=6)
.Delete
End With
End If
isSup = InStr(cel.Value, "<sup>") > 0
isSub = InStr(cel.Value, "<sub>") > 0
Loop
Next cel
For i = 2 To lastRow
If Not IsEmpty(Cells(i, 2)) Then
Cells(i, 2).Offset(0, -1).Value = Cells(i, 2).Offset(0, -1).Value & Cells(i, 2).Value
End If
Next i
End Sub
Edit2:几个笔记。如上所述,它将使用len&gt;分隔单元格。 255成两个单元格。然后它尝试将第二个单元格文本添加回COl。 A. 然而,问题:当您将第二个单元格中的文本与第一个单元格合并时,Degree符号会重置为o
而不是上标。
存在几种解决方法。一种是使用查找/替换(如我在顶部所示),并且当您要替换新的HTML元素时,只需添加到此列表即可。或者,您可以为长度不超过255的所有单元格运行sub,并自行分隔较大的单元格。 (请注意,截至目前,代码会将</sup>
分为</su
和p>
,如果最后的长度> 255 ...我将继续努力查看是否有一个更好的解决方案。