+ Reply to Thread
Results 1 to 6 of 6

VBA for all sheets instead of just one sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    04-12-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    VBA for all sheets instead of just one sheet

    Hello everyone,

    I have a macro that inserts a picture in the right header of the worksheet that is currently active. I want it to be so that it puts it in all worksheets, or even all selected worksheets, instead of just one worksheet at a time.

    Here is my code:

    Sub InsertLogo()
    With ActiveSheet.PageSetup.RightHeaderPicture
            .Filename = "Picture/jpg"
            .ColorType = msoPictureAutomatic
        End With
    
        ' Enable the image to show up in the right header.
        ActiveSheet.PageSetup.RightHeader = "&G"
    End Sub
    I have been trying to figure this out all afternoon. Thank you so much for the help!

  2. #2
    Registered User
    Join Date
    04-12-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA for all sheets instead of just one sheet

    its picture.jpg - ignore the erroneous slash. The code works great I just want to tweak it to do it to all sheets.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA for all sheets instead of just one sheet

    Untested

    Sub InsertLogo()
    
    Dim sh as worksheet
    
    For each sh in worksheets
    
    With sh.PageSetup.RightHeaderPicture
            .Filename = "Picture.jpg"
            .ColorType = msoPictureAutomatic
        End With
    
        ' Enable the image to show up in the right header.
        sh.PageSetup.RightHeader = "&G"
    Next
    End Sub
    Last edited by abousetta; 04-12-2012 at 03:46 PM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  4. #4
    Registered User
    Join Date
    04-12-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA for all sheets instead of just one sheet

    Thank you, it looks promising. I just tested it and it returns "Compile error: For without Next" I wish I knew what that meant but I'm new to this.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA for all sheets instead of just one sheet

    Sorry about that... forgot the Next. I added it above.

  6. #6
    Registered User
    Join Date
    04-12-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA for all sheets instead of just one sheet

    It worked! You've saved me a ton. Thank you so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1