I am trying to copy and paste shape from sheet2 to sheet1 in VBA. However, after pasting multiple times. I notice that the shapes are sharing the same name, which means they are sharing the same macro and the macro is only applied to the first shape pasted with the same name. To solve this, I have used the following code to randomly regenerate the shape's name in sheet1 after copying.
Public Function RL()
Dim Rand As String
Dim i As Integer, XSet As Integer
Dim MyCase As Integer
Application.Volatile
MyCase = 38: XSet = 85
Do
i = i + 1
Randomize
Rand = Rand & Chr(Int((XSet) * Rnd + MyCase))
Loop Until i = 5
RL = "X" & Rand
End Function
However, I found that there may still be cases where random name RL are not unique in sheet1,
Although it is pretty rare, this did happen quite a few times.
Thus, I decided to add in a check inside the function RL() to see whether the RL generated have already existed in sheet1. However, I found this quite time consuming as there are a lot of shapes in sheet1.
Is there any efficient way so that I could copy and paste uniquely?
Nbr= Paste_Sheet.Shapes.CountasRL="X" & Nbrmay solve your problem. See my answer below. - Takedasama