PDA

View Full Version : Can't name a command button with code



jrdnoland
08-25-2008, 04:54 AM
I'm having troubles naming command buttons that I paste into a row with code.
A portion of the code is included.

I search through some rows and find the last row containing a command button in it.
For Position = 198 To 500
Worksheets("Test Plans").Shapes("cmdESD" & Position).Select
' MsgBox ActiveSheet.Shapes("cmdESD" & Position).TopLeftCell.row
If Err.Number = 0 Then
'the object exists
Else
'the object doesn't exist
ObjLocation = ActiveSheet.Shapes("cmdESD" & Position - 1).TopLeftCell.row
Exit For
End If
Next Position

Next I copy the row to capture all the formating on the row.

'Copy row
Rows(ObjLocation).Select
Selection.Copy

I then paste into the next row

'Paste row
Range("A" & ObjLocation + 1).Select
Worksheets("Test Plans").Paste

I then copy the first of two command buttons in the copied row. Buttons are named cmdWord200 and cmdESD200, where 200 is the row number they are on.


Worksheets("Test Plans").Shapes("cmdWord" & ObjLocation).Select
Application.CutCopyMode = False
Selection.Copy

I then paste the copied button into the pasted row


Range("C" & ObjLocation + 1).Select
Worksheets("Test Plans").Paste 'Select the pasted object
'Name the selected object
Worksheets("Test Plans").Shapes("CommandButton1").Name = "cmdWord" & CStr(ObjLocation + 1)

This is repeated for the second command button.


Worksheets("Test Plans").Shapes("cmdESD" & ObjLocation).Select
Application.CutCopyMode = False
Selection.Copy
Range("C" & ObjLocation + 1).Select
Worksheets("Test Plans").Paste
'Rename command buttons
Worksheets("Test Plans").Shapes("CommandButton2").Name = "cmdESD" & CStr(ObjLocation + 1)

The problem is that the first command button is never renamed, the second one always seems to work.

When I step through the code I get the message "Can't Enter Break Mode At this time".

I can't find any duplicate names and I can name the button manually.

Am I missing something?

Thanks!