在_ExcelTips_的其他问题中,您将学习如何使用“照相机”工具捕获工作表不同部分的动态图片。您可能一直想知道如何在VBA中使用相同的功能。

关于此问题的文档并不十分清楚,但是以下宏显示了一般过程:

Sub DoCamera()

Dim MyPrompt As String     Dim MyTitle As String     Dim UserRange As Range     Dim OutputRange As Range

Application.ScreenUpdating = True

'Prompt user for range to capture     MyPrompt = "Select the range you would like to capture."

MyTitle = "User Input Required"

On Error Resume Next     Set UserRange = Application.InputBox(Prompt:=MyPrompt, _         Title:=MyTitle, Default:=ActiveCell.Address, Type:=8)

If UserRange Is Nothing Then End     On Error GoTo 0

'Copy range to Clipboard as picture     UserRange.CopyPicture

'Prompt user for range to paste to     MyPrompt = "Select the range on which you would like to paste."

MyTitle = "User Input Required"

On Error Resume Next     Set OutputRange = Application.InputBox(Prompt:=MyPrompt, _         Title:=MyTitle, Default:=ActiveCell.Address, Type:=8)

If OutputRange Is Nothing Then End     On Error GoTo 0

'Paste picture to output range     OutputRange.PasteSpecial     Selection.Formula = UserRange.Address End Sub

此宏提示您指定要复制的范围,然后将其作为图片复制到剪贴板,并提示您将其粘贴到何处。

粘贴后,宏的最后一行是使“照片”动态化的关键,就像使用“摄影机”工具手动进行的操作一样。 PasteSpecial命令实际上粘贴图片,并且粘贴的图片保持选中状态。设置所选内容(图片)的Formula属性会导致图形的动态性质。

注意:

如果您想知道如何使用此页面(或_ExcelTips_网站上的任何其他页面)中描述的宏,我准备了一个特殊页面,其中包含有用的信息。

_ExcelTips_是您进行经济高效的Microsoft Excel培训的来源。

本技巧(2207)适用于Microsoft Excel 97、2000、2002和2003。可以在以下功能区中为Excel的功能区界面(Excel 2007及更高版本)找到本技巧的版本: