How to take a picture of a specified range in a specified worksheet

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
Mr. Excel People:

Using VBA I need to take an unlinked "picture" (i.e., like that from the camera tool) of a specific range.

When recording I selected the range then clicked on the camera tool. The recorder said

VBA Code:
Activesheet.Shapes.AddShape(, 147#, 133.5, 72#, 72#)

But I do not know how to determine those numbers for the AddShape method.

The worksheet (object) is wsRawDataSheet and the range (object) to take picture of is rAllDataWithHeaders

Thanks a lot!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
OK so I figured out how to specify Top, Left, Height and Width. I've "created a shape" (rectangle) with default fill color but I cannot figure out how to make the shape a picture of a range like the camera tool.

Maybe I need a different MsoAutoShapeType?

Digression, the way the recorder creates code when I using the camera tool -- without the MsoAutoShapeType parameter -- causes an error.
VBA Code:
Activesheet.Shapes.AddShape(, 147#, 133.5, 72#, 72#)

Here is the sub that I have:

VBA Code:
Sub MakePicture(prPictureRange As Range)

    Dim dLeft As Double
    Dim dTop As Double
    Dim dHeight As Double
    Dim dWidth As Double
    
    Dim oPicture As Object
    
    With prPictureRange
        dLeft = .Left
        dTop = .Top
        dWidth = .Width
        dHeight = .Height
        .Copy
    End With
    
    Set oPicture = ActiveSheet.Shapes.AddShape(msoShapeRectangle, dLeft, dTop, dWidth, dHeight)
    
    Application.CutCopyMode = False
    
End Sub
 
Upvote 0
I have it working now. Here is the key part of the code:

VBA Code:
Sub MakePicture(prPictureRange As Range)

    Dim dLeft As Double
    Dim dTop As Double
    Dim dHeight As Double
    Dim dWidth As Double
    
    Dim oPicture As Object
    
    prPictureRange.Copy
    
    Set oPicture = ActiveSheet.Pictures.Paste
    
    Application.CutCopyMode = False
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,168
Messages
6,123,402
Members
449,098
Latest member
ArturS75

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top