Hi,
Using the code you've provided, is there a way to set the destination cell relative to a command button that is controlling the macro? Essentially, I would like to copy a button from cell "Q8" and paste it two cells to the right of the command button I have pressed. This command button could be in any cell so it would be great if I could paste the new command button relative to the called button.
I've tried the following code but I get a run-time error that I don't quite understand. The only parts that I've changed are the "Set sourceRange" and Set destinationRange".
Sub test()
Dim sourceRange As Range, sourceCell As Range
Dim destinationRange As Range
Dim rNum As Long, cNum As Long
Dim Loffset As Single, Toffset As Single
Dim coveringButton As Shape
Set sourceRange = Sheet1.Range("Q8"): Rem adjust
Set destinationRange = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(0, 2): Rem adjust
With sourceRange
Rem copy buttons
For rNum = 1 To .Rows.Count
For cNum = 1 To .Columns.Count
With .Cells(rNum, cNum)
Set coveringButton = ButtonCovering(.Cells)
If coveringButton Is Nothing Then
Rem do nothing
Else
Loffset = coveringButton.Left - .Left
Toffset = coveringButton.Top - .Top
coveringButton.Copy
With destinationRange.Parent
.Paste
With .Shapes(.Shapes.Count)
.Top = destinationRange(rNum, cNum).Top + Toffset
.Left = destinationRange.Cells(rNum, cNum).Left + Loffset
End With
End With
End If
End With
Next cNum
Next rNum
Rem copy cells
.Copy Destination:=destinationRange
End With
End Sub
Function ButtonCovering(aCell As Range) As Shape
Dim oneShape As Shape
For Each oneShape In aCell.Parent.Shapes
With oneShape
If .Type = msoFormControl Then
If .FormControlType = xlButtonControl Then
If ((aCell.Left < .Left + .Width / 2) And (.Left + .Width / 2 <= aCell.Left + aCell.Width)) _
And ((aCell.Top < .Top + .Height / 2) And (.Top + .Height / 2 < aCell.Top + aCell.Height)) Then
Set ButtonCovering = oneShape
Exit Function
End If
End If
End If
End With
Next oneShape
End Function
Bookmarks