简体   繁体   中英

Excel VBA Copy and reposition logos across multiple worksheets

I'm currently looking to put together a simple excel workbook filled with several dozen template forms we use (one per worksheet). As part of this I am looking to have a "master" sheet at the front which can with the press of a macro copy information into all the other worksheets.

Doing this for the cells has been easy, but I have had limited success in copying across any logos. My intention is to put my company's logo at the top right of the form and any client logos at the top left, hit a button and the 2-3 logos will appear in the same place across all the other worksheets.

I've managed to reposition the logos individually by name with the following code:

For Each Shape In Worksheet.Shapes
       If Shape.Name = "OurLogo" Then
        Shape.Left = (Wb1.Sheets("Inputs").Shapes("Our Logo").Left - 1)
        Shape.Top = (Wb1.Sheets("Inputs").Shapes("Our Logo").Top - 1)
        End If
        Next Shape

Where Our Logo is the original logo and OurLogo is the copied and named logo.

However this means I need the logos already copied into the correct cell in each workbook with the correct name. I've managed to copy the logo across all the worksheets, but not been able to rename them as I've been going, they are just assigned "Picture#" as a name. I tried including a rename in a similar "For Each" macro to copy it across all the sheets, but this only renamed one logo pasted into the active worksheet and didn't rename any in the remaining worksheets.

Any advice on how to copy and rename these pictures to multiple worksheets while also renaming them would be greatly appreciated. Alternatively I would accept copying first and a method of renaming after with a second macro.

Thanks in advance for your help.

I previously tried the How to copy a Shape to another worksheet (not as a picture)?, method but setting the destination cell simply dumped the logo in the top left corner, looking very untidy when I wanted it centred - hence the repositioning macro based on the position of the original logo. The method under that link also renamed the logo to "Picture#", creating the renaming requirement.

The 2nd result when Googling for "Excel VBA shape copy" point to a nifty question here at SE.

How to copy a Shape to another worksheet (not as a picture)?

Pretty sure that will solve your problem.

i have done this with 1 shape

Sub CopyAndRenameShape()

    Dim sh As Shape
    Dim wks As Worksheet

    Set sh = Sheet1.Shapes("shtTest")

    For Each wks In ThisWorkbook.Worksheets
        If wks.Name <> ActiveSheet.Name Then
            sh.Name = wks.Name & "_" & sh.Name
            sh.Copy
            wks.Paste
        End If
    Next wks
    sh.Name = "shtTest"
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM