Forum Discussion

GopiBalaraju's avatar
GopiBalaraju
Copper Contributor
Jul 26, 2024

CHANGING THE REFERENCE OF THE SHAPES WHEN THE SHAPES ARE DUPICATED

Hi all,

i have a format that i use to copy paste multiple times in my assignment using macros.

i see that copy pasting the shapes always take the reference to the cells declared in the first object. is there a way i use the position of the shape and select the cells accordingly?

for example, copy pasting the cells from a1:e46 creates the shapes again with the same names. and refer to the cells from a1:e33 if i save. infact, after i copy paste, i need it to select the cells from g1:g33

 

 

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    GopiBalaraju 

    To achieve dynamic referencing of shapes when duplicating them in Office 365 using VBA macros, you can adjust the properties of the newly created shapes based on their positions. This approach allows the duplicated shapes to reference different cell ranges as needed.

    Here’s a step-by-step guide on how to do this:

    1. Create the Macro to Copy and Paste Shapes:

    This macro will copy the shapes from one range, paste them, and then adjust their properties to reference a new cell range based on their positions.

    2. Adjust the Cell References Dynamically:

    After copying and pasting the shapes, you will need to calculate the new cell range and update the shapes' references accordingly.

    Here's an example VBA code snippet to illustrate this process:

    Vba Code is untested backup your file first.

    Sub DuplicateShapesAndChangeReference()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name
        
        Dim originalShape As Shape
        Dim newShape As Shape
        Dim originalRange As Range
        Dim newRange As Range
        
        ' Define the original range
        Set originalRange = ws.Range("A1:E33")
        
        ' Copy and paste the shapes from the original range
        For Each originalShape In ws.Shapes
            If Not Intersect(originalShape.TopLeftCell, originalRange) Is Nothing Then
                originalShape.Copy
                ws.Paste Destination:=ws.Cells(originalShape.TopLeftCell.Row, originalShape.TopLeftCell.Column + 6)
            End If
        Next originalShape
        
        ' Adjust the references of the newly pasted shapes
        For Each newShape In ws.Shapes
            If Not Intersect(newShape.TopLeftCell, ws.Range("G1:G33")) Is Nothing Then
                ' Change the reference of the shape as per the new position
                ' Assuming you're adjusting a cell reference in the shape text
                ' Example: change "A1:E33" to "G1:G33"
                
                ' You may need to adjust the logic depending on what the shapes reference
                newShape.TextFrame2.TextRange.Text = Replace(newShape.TextFrame2.TextRange.Text, "A1:E33", "G1:G33")
            End If
        Next newShape
    End Sub

    Explanation:

    1. Set the Worksheet:
      • Assign the worksheet where the shapes are located.
    2. Define the Original Range:
      • Specify the range of cells that the original shapes reference.
    3. Copy and Paste Shapes:
      • Loop through each shape and check if it intersects with the original range.
      • Copy the shape and paste it to a new location (adjusted by column offset, in this case, 6 columns to the right).
    4. Adjust New Shape References:
      • Loop through each new shape and adjust its reference based on its new position.
      • In this example, the text within the shapes is modified to reflect the new cell range.

    Important Notes:

    • Ensure that the logic for adjusting the shape references matches your specific requirements. The above example assumes that the shapes contain text referencing cell ranges and that you need to adjust this text.
    • Modify the column offset (in the Destination argument of the Paste method) as per your needs.
    • You might need to add additional checks or modify the Replace function based on how the shapes are originally referencing the cell ranges.

    This approach should help you dynamically update the references of the duplicated shapes based on their new positions in the worksheet. The text, steps and code were created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

    Was the answer useful? Mark as best response and Like it!

    This will help all forum participants.

    • GopiBalaraju's avatar
      GopiBalaraju
      Copper Contributor

      Hi NikolinoDE ,

      thank you for your well explained response.

      I would like to make clear with my requirement with a better explanation.

      i have a sheet (source sheet) with the contents (template in my case) as in the first screenshot. as we see, there are some shapes in it. with a macro assigned to a button that doesn't change its position, i copy from the source sheet and paste in the target sheet. the first range always starts from a1:e49. for copy pasting the second time, i find the first cell in the last empty column and paste it. the second time the range will be from f1:j49. its not limited to just 2times but goes on.

      my idea is to link the positions of the shapes to the column that i calculated above to paste the template.

      save and save all are the shapes i used to run the macro. they are dedicated to individual users.

      hope to hear from you. thanks again

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        GopiBalaraju 

        To dynamically link the positions of shapes to the calculated column for pasting the template in the target sheet, we need to create a macro that:

        1. Finds the next empty column.
        2. Copies the template (including shapes) from the source sheet.
        3. Pastes it into the target sheet in the calculated column.
        4. Adjusts the references of the shapes according to their new positions.

        Here is a VBA code snippet to achieve this:

        Vba Code is untested backup your file first.

        Sub CopyTemplateWithShapes()
            Dim wsSource As Worksheet
            Dim wsTarget As Worksheet
            Dim sourceRange As Range
            Dim targetRange As Range
            Dim lastCol As Long
            Dim shape As Shape
            
            ' Set your source and target sheets
            Set wsSource = ThisWorkbook.Sheets("SourceSheet") ' Change to your source sheet name
            Set wsTarget = ThisWorkbook.Sheets("TargetSheet") ' Change to your target sheet name
        
            ' Define the range of the template
            Set sourceRange = wsSource.Range("A1:E49")
            
            ' Find the last used column in the target sheet
            lastCol = wsTarget.Cells(1, wsTarget.Columns.Count).End(xlToLeft).Column
            
            ' Calculate the starting column for the new template range
            If lastCol = 1 And wsTarget.Cells(1, 1).Value = "" Then
                ' If the first cell is empty, start from column 1
                lastCol = 0
            End If
            Dim startCol As Long
            startCol = lastCol + 1
        
            ' Define the target range
            Set targetRange = wsTarget.Cells(1, startCol)
            
            ' Copy and paste the template range
            sourceRange.Copy
            targetRange.PasteSpecial Paste:=xlPasteAll
        
            ' Adjust shape positions
            For Each shape In wsSource.Shapes
                If Not Intersect(shape.TopLeftCell, sourceRange) Is Nothing Then
                    shape.Copy
                    wsTarget.Paste
                    With wsTarget.Shapes(wsTarget.Shapes.Count)
                        ' Calculate the new position
                        .Top = wsTarget.Cells(shape.TopLeftCell.Row, startCol).Top
                        .Left = wsTarget.Cells(shape.TopLeftCell.Row, startCol).Left
                    End With
                End If
            Next shape
            
            ' Clear the clipboard
            Application.CutCopyMode = False
        End Sub

        Explanation:

        1. Set Source and Target Sheets:
          • Assign the worksheets for the source and target.
        2. Define the Template Range:
          • Specify the cell range that includes the template.
        3. Find the Last Used Column:
          • Determine the last used column in the target sheet.
          • Adjust for cases where the first cell might be empty.
        4. Calculate the Starting Column:
          • Determine the starting column for the new template.
        5. Copy and Paste Template Range:
          • Copy the template range from the source sheet.
          • Paste it into the target sheet starting from the calculated column.
        6. Adjust Shape Positions:
          • Loop through each shape in the source sheet.
          • Check if the shape intersects with the template range.
          • Copy and paste the shape into the target sheet.
          • Adjust the shape’s position based on the new starting column.

        Usage:

        • Assign the Macro to a Button:
          • In the source sheet, assign the CopyTemplateWithShapes macro to a button that users can click to copy and paste the template with shapes to the target sheet.

        This approach ensures that shapes are dynamically linked and positioned according to the calculated column in the target sheet. The text, steps and the code were created with the help of AI

         

        My answers are voluntary and without guarantee!

         

        Hope this will help you.

Resources