replace单元格replacefunction – VBA

我目前有几十列,每行有几百行。 我所调用的其中一个subprocess会修改从Web上托pipe的XML进入的单元的数据。 当前的方法是有效的,但是它会逐渐变慢,因为它会逐个单元地进行更改。 这是我的代码:

Private Sub fixEnt(listCol As ListColumn) 'fixes the HTML/XML entities Dim rng As Range On Error Resume Next Set rng = listCol.DataBodyRange.SpecialCells(xlCellTypeConstants) On Error GoTo 0 If Not rng Is Nothing Then For Each areaItem In rng.Areas 'iterate over the arrays in the Areas array For Each cell In areaItem 'iterate over the values in the Item array cell.Value = decodeEnt(cell.Value) Next Next End If End Sub 

哪个调用decodeEnt:

 Private Function decodeEnt(cellVal As String) As String Dim tempStr$ 'holds new value after replace tempStr = Replace(cellVal, """, Chr(34)) 'Chr(34) is a " tempStr = Replace(tempStr, "'", "'") tempStr = Replace(tempStr, "&", "&") tempStr = Replace(tempStr, "<", "<") tempStr = Replace(tempStr, ">", ">") tempStr = Replace(tempStr, " ", " ") tempStr = Replace(tempStr, "#", "#") tempStr = Replace(tempStr, "&nbsp;", " ") tempStr = Replace(tempStr, "&lt;", "<") tempStr = Replace(tempStr, "&gt;", ">") tempStr = Replace(tempStr, "&quot;", Chr(34)) tempStr = Replace(tempStr, "&apos;", "'") tempStr = Replace(tempStr, "&amp;", "&") tempStr = Replace(tempStr, "&ndash;", "–") tempStr = Replace(tempStr, "ü", "ü") tempStr = Replace(tempStr, "&deg;", "°") tempStr = Replace(tempStr, "&auml;", "ä") tempStr = Replace(tempStr, "&uuml;", "ü") tempStr = Replace(tempStr, "&rsquo;", "'") decodeEnt = tempStr 'Return modified string End Function 

有没有更快捷的方式来执行该操作? 在rng.Areas数组中可能会同时修改所有数据? 速度是这个项目的关键,但在这种情况下,我没有想法。

谢谢

编辑:进一步澄清有关该项目。 它从另一个工具的API中导入XML文件,并将其保存到Excel中的表中。 我有其他代码刷新连接,附加XML(新的和旧的)的所有数据。 刷新过程完成后,将开始数据修改,包括修复单元格中的HTML / XML实体并修改date格式。 修改完成后,它会删除重复的行(因为在刷新时没有办法只添加新的数据)。

希望这个清除任何混乱。

我怀疑下面会更快(一次做所有细胞):

 Sub test() Dim replaceThis() Dim withThis() replaceThis = Array("&lt;", "&gt;") ' etc withThis = Array("<", ">") ' etc Application.ScreenUpdating = False For i = LBound(replaceThis) To UBound(replaceThis) 'MsgBox "replacing " & replaceThis(i) & " with " & withThis(i) Range("A1:A5").Replace What:=replaceThis(i), Replacement:=withThis(i), _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False Next i Application.ScreenUpdating = True End Sub 

注意 – 你将要创build一个包含你所有replace的数组,我硬编码的范围:你需要做的variables。 但是看到你的代码,我想你可以从这里弄清楚。

有一个可能更快的范围替代function。

对于一般性能提示(特别是ScreenUpdating,Calculation and“在单个操作中读取/写入大块单元格”):

http://blogs.office.com/2009/03/12/excel-vba-performance-coding-best-practices/

(根据我的经验,主要帮助的是禁用Application.ScreenUpdating,在单个操作中取消激活Application.Calculation或读取/写入大块单元格)