删除HTML代码的VB代码不断崩溃Excel〜40k数据集
这是我的VBA代码:
Sub RemoveTags() Dim r As Range Selection.NumberFormat = "@" 'set cells to text numberformat With CreateObject("vbscript.regexp") .Pattern = "\<.*?\>" .Global = True For Each r In Selection r.Value = .Replace(r.Value, "") Next r End With End Sub
它确实删除了我的单元格中的所有标记语言,但是当我运行〜40k条logging时会崩溃。 我的代码有问题,或者我应该改变Excel设置?
我的猜测是Excel试图将文本写回到单元格时发生了变化。 这里有几件事你可以尝试:
- 使用
.Value2
而不是.Value
来处理原始值。 - 在文本前添加一个单引号。 它不会出现,但它将确保文本格式
- 使用一个不遵循的模式,而不是一个非贪婪的,以确保处理换行符。
Sub RemoveTags() Dim values(), r As Long, c As Long, re As Object ' load the values in an array values = Selection.Value2 ' create the regex Set re = CreateObject("vbscript.regexp") re.pattern = "<[^>]*>" re.Global = True ' remove the tags for each value For r = 1 To UBound(values, 1) For c = 1 To UBound(values, 2) values(r, c) = "'" & re.replace(values(r, c), vbNullString) Next Next ' write the values back to the sheet Selection.Value2 = values End Sub