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
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,013
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!
 

Forum statistics

Threads
1,082,253
Messages
5,364,045
Members
400,776
Latest member
JimmyLee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top