回答
我说,忘了SpecialCells
。只需将需要测试的所有单元格加载到Variant数组中。然后循环播放该数组,并进行上限。这是非常有效的,与在表单中循环单元格相反。最后,将其写回表单。
使用包含介于0和2之间的随机值的50,000个单元格,此代码在我的古董笔记本电脑上以0.2秒运行。
额外的好处是,这是相当清晰和可读的代码,并且您可以完全控制将操作的范围。
Dim r As Range
Dim v As Variant
Set r = Sheet1.UsedRange
' Or customise it:
'Set r = Sheet1.Range("A1:HZ234") ' or whatever.
v = r ' Load cells to a Variant array
Dim i As Long, j As Long
For i = LBound(v, 1) To UBound(v, 1)
For j = LBound(v, 2) To UBound(v, 2)
If IsNumeric(v(i, j)) And v(i, j) > 1 Then
v(i, j) = 1 ' Cap value to 1.
End If
Next j
Next i
r = v ' Write Variant array back to sheet.
+1。 *但是*具有'SpecialCells'的变体再次变得更好。为什么用一把漂亮的面包刀处理2D范围内的所有细胞时,只能使用手术刀处理数字常量(确保时间从微小到微观,但效率很高:) :) – brettdj 2012-01-12 12:01:27
谢谢!我想我会用这个。看起来最可读和最短。 – brunosan 2012-01-12 13:30:41
@brettdj:我的比喻是:为什么使用复杂的机器人工具进行远程手术时,病人在隔壁的房子里?只需将患者带入干净,无菌的手术室,然后将空白处使用您选择的刀,然后将其送回家。在* all *单元格上完成的唯一“处理”是读取,检查一个布尔值('IsNumeric(v(i,j))')并写回。 – 2012-01-12 18:38:29
循环的危害是什么?我刚刚在39900个单元格范围内测试了这段代码,并以2秒的速度运行。
Sub Sample()
Dim Rng As Range, aCell As Range
Set Rng = Cells.SpecialCells(xlCellTypeConstants, xlNumbers)
For Each aCell In Rng
If aCell.Value > 1 Then aCell.Value = 1
Next aCell
End Sub
我唯一担心的是使用SpecialCells的,因为它们是不可预知的,所以我很少使用它们。
也有看看这个知识库文章:http://support.microsoft.com/?kbid=832293
锡德,你在这些细胞中有40K数字吗? 8192区域的限制 - 在我的体验中很少被破坏 - 已在xl2010 – brettdj 2012-01-12 01:10:58
中删除是的(精确到39900)从B7到T2106(在Excel 2003中测试过) – 2012-01-12 07:18:13
循环越过单元格有什么危害?那么,2秒就比下一个最好的解决方案差一个数量级。如果下次有400,000个值需要检查怎么办?如果OP必须运行代码100次会怎样? – 2012-01-12 18:41:14
下面这种方法避免了通过细胞循环的细胞 - 而这是显著长于范围循环代码我同意你的喜好,以避免细胞通过细胞范围循环,其中可能
我已经更新从A fast method for determining the unlocked cell range我的代码,以提供由细胞循环方法的非细胞
- 代码检查该
SpecialCells(xlCellTypeConstants , xlNumbers)
在片材吨存在ø被更新(错误处理应始终使用与SpecialCells
- 如果这些细胞存在,工作片被创建 ,和式被插入到范围为第1步建立一个故意错误(1/0 )如果在主片的值是> 1
SpecialCells(xlCellTypeFormulas, xlErrors)
返回的单元格范围从工作片其中的值均大于1(成rng3
)在
rng3
所有区域都设定为1与rng3.Value2=1
Sub QuickUpdate() Dim ws1 As Worksheet Dim ws2 As Worksheet Dim rng1 As Range Dim rng2 As Range Dim rng3 As Range Dim lCalc As Long Set ws1 = ActiveSheet On Error Resume Next Set rng1 = ws1.Cells.SpecialCells(xlConstants, xlNumbers) On Error GoTo 0 'exit if there are no contants with numbers If rng1 Is Nothing Then Exit Sub 'disable screenupdating, event code and warning messages. 'set calculation to manual With Application .ScreenUpdating = False .EnableEvents = False .DisplayAlerts = False lCalc = .Calculation .Calculation = xlCalculationManual End With ws1.Copy After:=Sheets(Sheets.Count) Set ws2 = ActiveSheet 'test for cells constants > 1 ws2.Cells.SpecialCells(xlConstants, xlNumbers).FormulaR1C1 = "=IF('" & ws1.Name & "'!RC>1,1/0,'" & ws1.Name & "'!RC)" On Error Resume Next Set rng2 = ws2.Cells.SpecialCells(xlCellTypeFormulas, xlErrors) On Error GoTo 0 If Not rng2 Is Nothing Then Set rng3 = ws1.Range(rng2.Address) rng3.Value2 = 1 Else MsgBox "No constants < 1" End If ws2.Delete 'cleanup user interface and settings With Application .ScreenUpdating = True .EnableEvents = True .DisplayAlerts = True lCalc = .Calculation End With 'inform the user of the unlocked cell range If Not rng3 Is Nothing Then MsgBox "Cells updated in Sheet " & vbNewLine & ws1.Name & vbNewLine & " are " & vbNewLine & rng3.Address(0, 0) Else MsgBox "No cells updated in " & ws1.Name End If End Sub
- 1. 选择单行表的所有单元格的值,并在SQL
- 2. 选择具有特定行中的值的所有单元格
- 3. 让所有选中的单元格值
- 4. 选择单元格条目上的所有Silverlight DataGrid单元格文本
- 5. mysql使用上次修订版的值选择所有订单
- 6. 在UITableView中一次选择一个单元格
- 7. 从活动单元格中选择所有单元格直到空白单元格并粘贴值
- 8. 在Excel/VBA中选择合并单元格下的所有单元格
- 9. 在GridView中选择单元格的值
- 10. 需要选择WorkBOOK中的所有单元格以将所有“=”替换为“####”
- 11. 选择一个单元格一次,但一切
- 12. 如何在swift中一次只选择两个UITableview单元格
- 13. 在Excel中选择具有特定值的单元格的相邻单元格
- 14. SQL:选择基于另一个单元格的值单元格的值
- 15. 如何CSS选择除IE7/8上最后一个单元格之外的所有单元格?
- 16. 选择表格内的所有单元格
- 17. jquery选择html表格单元格内的所有标签
- 18. 在primefaces中一次性选择/取消选择所有元素<p:selectCheckboxMenu>?
- 19. 选择列上最后一个单元格上方的单元格
- 20. 是否可以选择条件格式为true的所有单元格?
- 21. 检索数据表单元格值以选择选项框
- 22. 在iOS中选择UITableView中的所有单元即使不可见单元格
- 23. 选择表格单元格
- 24. 选择哪些单元格可以活动单元格
- 25. 根据单元格中的值选择表单元格
- 26. 从列值中选择单元格
- 27. 只选择值为VBA的单元格
- 28. 如何选择单元格的值?
- 29. 根据单元格值选择行
- 30. 选择基于单元格值
我不认为有任何现成的方式来做到这一点。你将不得不做*一些*循环... – 2012-01-11 23:24:05