【发布时间】:2016-03-11 11:25:23
【问题描述】:
我有大约 40 个电子表格,每个电子表格最多包含 300k 行 x 93 列(目前)。这大约是 11 亿个数据点。我需要检查每个单元格,并确定该单元格是否包含 8 个特殊字符之一,这些字符在导入电子表格时被搞砸了。
这是一项需要每天多次运行的任务,以及许多其他步骤。因此,我正在寻找一种使用 VBA 的方法。我有以下代码:
Sub Hide_All_Sheets()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
Dim k As Integer
Dim t As String
Dim x As Integer
k = Sheets.Count
x = 1
While x <= k
t = Sheets(x).Name
If t = "Launch Screen" Or t = "Equiv sheet" Then
x = x + 1
ElseIf t = "Summary_1" And Worksheets("Launch Screen").Range("N5") = "1" Then
Sheets(x).Visible = True
x = x + 1
Else
Cells.Replace What:="ö", Replacement:=Chr(214), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="ü", Replacement:=Chr(220), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="ä", Replacement:=Chr(220), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="ß", Replacement:=Chr(223), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="è", Replacement:=Chr(200), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="Ü", Replacement:=Chr(223), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="Ä", Replacement:=Chr(223), LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Sheets(x).Visible = False
x = x + 1
End If
Wend
End Sub
唯一的问题是它将加载过程从 20 秒变为 900 秒。
我想知道有没有办法更快地做到这一点?特别是如果有一种方法可以运行手动 CTRL-H 进程,并在所有电子表格中替换,但使用 VBA?
【问题讨论】:
-
我认为每个工作簿的速度都差不多。当您说“电子表格”时,您是指工作表还是工作簿?如果是后者,您是否考虑过循环浏览 40 个工作簿而不是一一运行?如果工作簿名称和路径可变,请检查 FileSystemObject:msdn.microsoft.com/en-us/library/aa242706(v=vs.60).aspx
-
工作表 - 很抱歉在写标题时分心了,无法更改。代码确实循环,它只需要很长时间。没有检查异常字符的宏的整个运行时间仅为 840 秒。
-
也许确定使用的范围并替换它而不是所有单元格?
-
并且不修复导入代码页的原因是......?
-
@Jeeped 导入时同样的问题。它是由一个系统生成的数据提取格式。 Excel 无法识别字符的编码。