VBA to insert picture into multiple worksheets relative to Print_area

MuppetReaper

New Member
Joined
Jun 14, 2013
Messages
30
I want to be able to insert a picture into each worksheet of a workbook, but I don't want the picture in a cell, I want it in a location relative to the Print_area (as the number of columns changes in each ws)
The picture can be the same size in each ws, but just needs to be located in the top right (with a small margin) of the ws.
And I need it to loop through all ws's with a defined Print_area
Is this at all possible?
All I can seem to find is to insert into a cell, which I don't think will work in this case, but am willing to be proven wrong!
Thanks all
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
20,235
Office Version
  1. 365
Platform
  1. Windows
Try...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Sub[/COLOR] InsertPicturesAtTopRightPrintArea()

    [COLOR=darkblue]Dim[/COLOR] sPathAndFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sPrintArea [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] ws [COLOR=darkblue]As[/COLOR] Worksheet
    [COLOR=darkblue]Dim[/COLOR] shp [COLOR=darkblue]As[/COLOR] Shape
    [COLOR=darkblue]Dim[/COLOR] rPrintArea [COLOR=darkblue]As[/COLOR] Range
    
    [COLOR=darkblue]Const[/COLOR] Gap [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR] = 5 [COLOR=green]'top and right margins[/COLOR]
    
    [COLOR=darkblue]If[/COLOR] ActiveWorkbook [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR] [COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]Sub[/COLOR]
    
    sPathAndFile = "C:\Users\Domenic\Pictures\about.jpg" [COLOR=green]'change path and filename accordingly[/COLOR]
    
    [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] ws [COLOR=darkblue]In[/COLOR] ActiveWorkbook.Worksheets
        sPrintArea = ws.PageSetup.PrintArea
        [COLOR=darkblue]If[/COLOR] Len(sPrintArea) > 0 [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]Set[/COLOR] rPrintArea = ws.Range(sPrintArea)
            [COLOR=darkblue]Set[/COLOR] shp = ws.Shapes.AddPicture(Filename:=sPathAndFile, linktofile:=msoFalse, _
                SaveWithDocument:=msoTrue, Left:=rPrintArea.Left, Top:=rPrintArea.Top + Gap, Width:=-1, Height:=-1)
            shp.Left = rPrintArea.Left + rPrintArea.Width - shp.Width - Gap
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]Next[/COLOR] ws
    
    MsgBox "Completed...", vbInformation
    
    [COLOR=darkblue]Set[/COLOR] ws = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] shp = [COLOR=darkblue]Nothing[/COLOR]
    [COLOR=darkblue]Set[/COLOR] rPrintArea = [COLOR=darkblue]Nothing[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hope this helps!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,530
Messages
5,837,904
Members
430,520
Latest member
VenkateshRajaganesan

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
Top