excel vba picture in cell to header

  • Hi


    I am trying to insert a picture in a header of an Excel File. It is clear to me how to do it from a local file in the hard disk, what I can not figure it out is:


    if the picture is one in a sheet from the same Workbook, as example:


    Code
    ActiveSheet.PageSetup.RightHeaderPicture = Sheets("datasheet").Shapes.Range(Array("myLogo"))
    
    
    With ActiveSheet.PageSetup
         .RightHeader = "&G"
    End With


    can anyone help me to figure it out?


    thanks in advance for your support

  • Re: excel vba picture in cell to header


    Save the shape as an image file and assign that file as the header picture:

  • Re: excel vba picture in cell to header


    What can I say :cheers:


    It works perfectly :music:



    thanks a lot, I was a bit lost on the way, just needed to think out the box to find the right way


    thanks for you kind support


    :ufo:

  • Re: excel vba picture in cell to header


    Just an additional note,


    the Whorksheet as to be UNPROTECTED, if it is protected then will not work, nothing that can not be easly solved, just add in the code:


    Code
    Sheets("datasheet").Unprotect Password:=xlwin
    
    
        'code itself
    
    
        Sheets("datasheet").Protect Password:=xlwin
  • Re: excel vba picture in cell to header


    Hello everyone! :)


    I hope it is ok to activate this old thread. I have the same problem as lepaka and the answer from John_w is working fine but only if I go through step by step. When the macro is running the picture does not appear in the header. What I know so far is that I have to give the macro some time to process the picture. I have tried to use DoEvents and Application.Wait but it did not work. Even when I tried both after each line! I am quite frustrated. Does anybody have a tip how the macro will work without me going through it step by step? Every help is appreciated a lot.


    :sheep:

  • Re: excel vba picture in cell to header


    Welcome to Ozgrid forums.


    I have several workbooks which use the Save_Object_As_Picture routine, and the code worked correctly when running Excel 2007 on a relatively slow Windows XP machine. However since upgrading to Excel 2016 and a fast (Intel i7 6700) Windows 10 machine I've found that the image/picture created by the code is empty.


    The solution is to Activate the temporary chart, like this:


    Code
    With temporaryChart
            .Activate                                'Required, otherwise image is blank with Excel 2016 or fast CPU (?)
            .Border.LineStyle = xlLineStyleNone      'No border
            .Chart.Paste
            .Chart.Export imageFileName
            .Delete
        End With

    If you tell us which version of Excel and Windows you're using and your computer's CPU this will help to pinpoint the reason why the Activate is needed.

  • Re: excel vba picture in cell to header


    Seriously, you are a legend! :congrats:


    I am using the latest Excel and got an Intel i5. The Activate did the magic and the picture finally appears.
    Thank you so much! :cheers:

  • Hi. I am a new user and even newer to Excel and I'm hooked. I require exactly what you described a vba picture header from a logo in the same workbook without having to refer to an external location. I just can't figure out how to make it work for me. The logo I want in my header is inserted on a sheet named "Tables" in my workbook. How do I change the code to fit my scenario. I know this is an old thread but any assistance would go a long way. Thanks.

  • Kannie, welcome to Ozgrid forum. You need to read the Forum Rules, one of which asks you to not "hijack" other threads with your question. Start your own Thread, refer to this one if you think it might help

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!