Joe*_*ato 5 excel vba vsto excel-vba excel-2010
这是我设计的一个例子,我创建这个来解释我遇到的问题.基本上我希望这段代码运行得比它快.在新的工作表上,单元格的每个循环都会快速启动,但如果让它运行到接近完成状态,然后再次运行,则每个单元格将达到100毫秒.在我的工作表中,我有16000个单元格,有很多这样的注释,每次代码运行时都会单独操作它们.在这个例子中,它们显然是完全相同的,但在实际应用中,每个都是不同的.
反正有没有让这个过程更快?
Option Explicit
Public Declare PtrSafe Function GetTickCount Lib "kernel32.dll" () As Long
Public Sub BreakTheCommentSystem()
Dim i As Integer
Dim t As Long
Dim Cell As Range
Dim dR As Range
Set dR = Range(Cells(2, 1), Cells(4000, 8))
Dim rStr As String
rStr = "ABCDEFG HIJK LMNOP QRS TUV WX YZ" & Chr(10)
For i = 1 To 5
rStr = rStr & rStr
Next i
For Each Cell In dR
t = GetTickCount
With Cell
If .Comment Is Nothing Then
.AddComment
Else
With .Comment
With .Shape.TextFrame.Characters.Font
.Bold = True
.Name = "Arial"
.Size = 8
End With
.Shape.TextFrame.AutoSize = True
.Text rStr
End With
End If
End With
Debug.Print (GetTickCount - t & " ms ")
Next
rStr = Empty
i = Empty
t = Empty
Set Cell = Nothing
Set dR = Nothing
End Sub
Run Code Online (Sandbox Code Playgroud)
更新12-11-2015,我希望在某些地方注意到以防任何人遇到它,我试图优化这么多的原因是因为VSTO不会让我添加带有所有这些注释的工作簿文件.在与Microsoft合作6个月后,现在这是VSTO和Excel中已确认的错误.
小智 8
根据MSDN Comments集合和Comment对象文档,您可以通过索引位置引用工作表中的所有注释并直接处理它们,而不是遍历每个单元格并确定它是否包含注释.
Dim c As Long
With ActiveSheet '<- set this worksheet reference properly!
For c = 1 To .Comments.Count
With .Comments(c)
Debug.Print .Parent.Address(0, 0) ' the .parent is the cell containing the comment
' do stuff with the .Comment object
End With
Next c
End With
Run Code Online (Sandbox Code Playgroud)
另外,根据Range.SpecialCells方法的官方文档,您可以使用xlCellTypeComments常量作为Type参数轻松确定工作表中的单元格子集.
Dim comcel As Range
With ActiveSheet '<- set this worksheet reference properly!
For Each comcel In .Cells.SpecialCells(xlCellTypeComments)
With comcel.Comment
Debug.Print .Parent.Address(0, 0) ' the .parent is the cell containing the comment
' do stuff with the .Comment object
End With
Next comcel
End With
Run Code Online (Sandbox Code Playgroud)
我仍然不清楚填写所有未注释的单元格后面的空白注释的原因但是如果你只想在工作表上使用注释,最好使用注释单元格的子集而不是循环遍历所有单元格寻找评论.
归档时间: |
|
查看次数: |
1934 次 |
最近记录: |