- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试自动化一份包含 5 个不同信息源的报告。我尝试使用 ListObjects 将不同表的 UNION 合并为一个表,除了复制第一个 ListObject 的第一列时,一切都工作正常。复制第一列大约需要 2 分钟,接下来的列只需不到 1 秒。
每次运行 VBA 脚本时,我都会删除目标表的所有行,以使用包含 0 行的 ListObject 启动 VBA 脚本。
我将尝试解释它是如何工作的:
Sub ProcesarPresupuesto()
'This is the first macro that process and copy the information of the first source
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
'<Here> I add several columns and process the information of this first source, I keep all the rows as values using the Function: AddColumnFormula (at the end of this example). I think this is not causing the problem.
'Then I fill all the Blanks Cells to avoid having empty cells in my final table.
Sheets("Origin").Select
Selection.CurrentRegion.Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "Null"
On Error GoTo 0
'When I have the ListObject ready I start copying the columns to the destination
Sheets("Destination").Select
Range("A1").Select
While ActiveCell.Value <> ""
Call CopyColumn("Origin", ActiveCell.Value, "Destination")
ActiveCell.Offset(0, 1).Select
Wend
End Sub
我认为这应该很快。如果我仅删除 Destination ListObject 的值并将行保留为空,则会立即复制第一列,因此我认为问题与 Excel 如何计算要添加到 ListObject 的第一行有关。当表为空时是否有更好的方法来复制列?我真的做错了什么吗?
这是函数 CopyColumn
Function CopyColumn(Origin, ColumnName, Destination)
Range(Origin & "[[" & ColumnName & "]]").Copy Destination:=Range(Destination & "[[" & ColumnName & "]]")
End Function
这是我用来处理列的函数
Function AddColumnFormula(DestinationSheet, TableName, ColumnName, Value)
Set NewColumn = Sheets(DestinationSheet).ListObjects(TableName).ListColumns.Add
NewColumn.Name = ColumnName
Set Rango = Range(TableName & "[[" & ColumnName & "]]")
Rango.Value = Value
Rango.Copy
Rango.PasteSpecial (xlPasteValues)
End Function
预先感谢您的时间和答复
最佳答案
我对您提供的文件进行了一些测试。虽然很慢,但一开始我没有计时。我看到了一些修改代码的机会,可能可以提高性能,并且计时器花了 1 分 16 秒。
我使用Debug.Print
尝试了更多的事情,并取得了不同程度的成功。语句来告诉我代码的哪一部分正在运行以及它们花费了多长时间。大多数执行每次大约 2 分钟,最慢的为 3 分 13 秒。
在最后 3 分 13 秒的尝试中,我将注意力集中到:
...CurrentRegion.SpecialCells(xlCellTypeBlanks)
这是可疑的,因为 CurrentRegion
和SpecialCells
方法可能很昂贵。将它们结合起来似乎会导致灾难。
我想我会尝试一个简单的迭代,只是为了比较性能,令我惊讶的是,我能够做一个简单的 For each
循环 42,000 行和 32 列数据,这将在大约 14 秒内一致执行,总运行时间约为 30 秒。
这是我用于循环的代码:
Dim cl As Range
'Debug.Print "For each ..." & Format(Now(), "hh:mm:ss")
For Each cl In wsP.ListObjects(1).DataBodyRange
If cl.Value = vbNullString Then cl.Value = "Null"
Next
'Debug.Print "End loop " & Format(Now(), "hh:mm:ss")
这是我的最后三个结果:
31 seconds:
Commencar a 21:09:25
For each ...21:09:38
End loop 21:09:52
CopiarColumnaListOBjectaVacia...21:09:52
Finito : 5/5/2014 9:09:56 PM
30 seconds:
Commencar a 21:10:23
For each ...21:10:36
End loop 21:10:49
CopiarColumnaListOBjectaVacia...21:10:49
Finito : 5/5/2014 9:10:53 PM
34 seconds:
Commencar a 21:18:42
For each ...21:18:55
End loop 21:19:09
CopiarColumna... 21:19:09
Finito : 5/5/2014 9:19:16 PM
我已将 XLSB 的修订版本保存在 Google 文档上,以便您可以完整查看。
https://drive.google.com/file/d/0B1v0s8ldwHRYZWhuTmRuaDJoMzQ/edit?usp=sharing
正如我所说,我确实对此子例程以及 RenombraColumna
进行了一些更改,但事后看来,虽然这些可能会提供一些效率,但我认为问题的根源在于 CurrentRegion.SpecialCells
.
我希望您不介意我修改了这个问题的标题以使其更适合特定问题。正如最初所述,这个问题不太可能帮助其他有相同症状的人。
关于excel - CurrentRegion.SpecialCells(xlCellTypeVisible) 太慢 - 提高性能的提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23395492/
我正在尝试在 Excel 2010 中为仅可见的行创建一个使用 VBA 的范围。我已经过滤掉了我不想要的值,我正在使用关键字 xlCelltypeVisible ,但是,当我对其进行测试时,该范围仍显
好吧,伙计们,这里完全困惑了...... 我在 Access 的 Excel 中有一个链接表。我正在尝试编写一个 vba 函数,该函数返回该表的给定列的过滤范围地址。请记住,我试图坚持使用结构化引用(
使用 Selection.SpecialCells(xlCellTypeVisible).Count 时要计算单列过滤数据,它适用于多行结果。但是当只显示 1 行时,它会给我一个溢出错误或者我得到 c
我正在尝试过滤有时可能导致没有数据的表。我正在尝试计算可见的行数以做出此决定。 如果只有一个标题行 - “无单元格” 如果有可见行 - “有提供者” 在下面的代码中,它似乎仍然计算过滤的行......
我有一个 Excel VBA 宏,每周运行一次。我有一段代码可以过滤出不同的数据,然后将剩余的单元格复制到不同的工作表中 以下是受影响的代码部分: dim data as worksheet dim
我正在尝试自动化一份包含 5 个不同信息源的报告。我尝试使用 ListObjects 将不同表的 UNION 合并为一个表,除了复制第一个 ListObject 的第一列时,一切都工作正常。复制第一列
基于question由 @Chips Ahoy 提出,我决定创建一个 UDF 来查找某个范围内可见单元格的 PercentRank。 虽然 @Chips 似乎对我的语法修正感到满意,但实际上我无法让我
我有一个过滤的列表对象,需要获取当前可见的行数。我使用此语句来获取行数: MySheet.ListObjects("MyListObject").DataBodyRange.SpecialCells(
这是我的代码 openWs.AutoFilterMode = False Range("A1").AutoFilter Field:=1, Criteria1:= _ ">=" & date1, Op
想知道是否有人注意到以下或类似代码的行为发生变化: .Range("CustomTable").SpecialCells(xlCellTypeVisible).EntireRow.Delete 我用它
我是一名优秀的程序员,十分优秀!