我有一个VBA脚本,可将注释添加到后台工作表中,效果很好。我遇到的问题是将其移至前端工作表。
我可以使用复制和粘贴特殊的xlPasteComments,但这确实减慢了更新过程。我在下面包含了将要重复的代码的一部分。如果我使用值,则它不包含注释(我留下来显示),并且我尝试了Dim将其分离出来,但这只会导致错误,即不支持对象。
If ws.Range("B9") = ("January") Then
Dim a As Long
Dim b As Long
ws.Range("J8:AN51").Value = area.Range("E2:AI45").Value
'This brings up a 438 runtime error (object doesnt support this propery
or method)
a = ws.Range("J8:AN51").Comments
b = area.Range("E2:AI45").Comments
a = b
'area.Range("E2:AI45").Copy
'ws.Range("J8:AN51").PasteSpecial xlPasteComments
ws.Range("J62:AN63").Value = area1.Range("E47:AI48").Value
ws.Range("J55:AN55").Value = area.Range("E52:AI52").Value
我已经在Google上进行了检查,但是它一直在显示如何在单元格中复制值,而我所关注的只是注释,(因为这些值已被复制)
我最初的想法是尝试将所有注释加载到VBA数组中,然后使用此注释数组写入其他工作表。
因此,我尝试从Chip Pearson的网站改编该技术,该技术正是针对单元格值进行的。
不幸的是,.comment.text
在具有多个单元格的范围内使用不会返回数组,这意味着该方法将不起作用。
这意味着,为了使用VBA将评论转移到另一张纸上,您需要一个接一个地遍历范围内的所有单元格(也许是一个集合)。尽管我确定这可以正常工作,但很可能不会比使用更快xlPasteComments
。
然后,我将决定使用通常的VBA技术,通过停用某些设置(例如自动计算,屏幕更新和事件)来使宏运行更快。这是我将如何实现它(包括一些错误处理)的示例:
Sub Optimize_VBA_Performance_Example()
Const proc_name = "Optimize_VBA_Performance_Example"
'Store the initial setting to reset it at the end
Dim Initial_xlCalculation_Setting As Variant
Initial_xlCalculation_Setting = Application.Calculation
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayStatusBar = False
.EnableEvents = False
End With
On Error GoTo Error_handler
'Your code
'Restore initial settings (before exiting macro)
With Application
.Calculation = Initial_xlCalculation_Setting
.ScreenUpdating = True
.EnableEvents = True
.DisplayStatusBar = True
End With
Exit Sub
Error_handler:
'Restore initial settings (after error)
With Application
.Calculation = Initial_xlCalculation_Setting
.ScreenUpdating = True
.EnableEvents = True
.DisplayStatusBar = True
End With
'Display error message
Call MsgBox("Run-time error '" & Err.Number & "': " & Err.Description & vbNewLine & _
"While running: " & proc_name & vbNewLine, _
vbCritical, "Error")
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句