Command Button Code (1 Viewer)

Tony1258

Registered User.
Local time
Today, 05:55
Joined
Jun 8, 2006
Messages
29
I have a macro that I created and I have copied the code from it to the code
area for a command button and when I execute the command button it does not
work but if I execute the macro it does. see the code for both below, I
receive the error Selected method of range class failed

Macro:

Private Sub CommandButton2_Click()
Range("A1:E1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ChDir "\\nysyrfsv04\shared\UGTREAS\P & C"
Workbooks.Open Filename:= _
"\\nysyrfsv04\shared\UGTREAS\P & C\Current Third Party.xls"
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWorkbook.Close
Range("A2:E2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("A2").Select
End Sub

Command:
Private Sub CommandButton3_Click()
Range("A2").Select
Workbooks.Open Filename:= _
"\\nysyrfsv04\shared\UGTREAS\P & C\Current Third Party.xls"
Range("A2:E2").Select ******Error here *******
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ActiveWindow.Close
ActiveSheet.Paste
Range("A2").Select
ActiveWorkbook.Save
End Sub
 

shades

Registered User.
Local time
Today, 04:55
Joined
Mar 25, 2002
Messages
516
Howdy. You don't want to put the code in the command button, only to refer to the code in the Normal VBA Module.
________
DISPENSARIES
 
Last edited:

Tony1258

Registered User.
Local time
Today, 05:55
Joined
Jun 8, 2006
Messages
29
How do you refer to the macro in the command button code?
 

shades

Registered User.
Local time
Today, 04:55
Joined
Mar 25, 2002
Messages
516
After I posted, I thought about what you meant by "command button"; then spurred by your PM, here is additional info.

1. Are you just wanting access to the code by clicking a button? If so, draw an object on the spreadsheet, right-click and choose "Assign Macro".

2. Or do you want a button on the toolbar? Right-click the toolbar, choose "Customize". In dialog box, choose middle tab "Commands", on the left side scroll down to "Macros", select and then on right side choose "Custom Menu Item", click-drag it up to the toolbar, and relase when you see the dark vertical line. Then right-click the button, and choose "Assign macro". Right-click again and choose "Name" and put your custom name there.

3. Or Forms or Control Toolbox buttons? Each has advantages.
________
TOYOTA TREKKER HISTORY
 
Last edited:

Tony1258

Registered User.
Local time
Today, 05:55
Joined
Jun 8, 2006
Messages
29
I am putting the button on the spreadsheet. When I drag it to the spread sheet and right clicthere is no option for assign macro.
 

Tony1258

Registered User.
Local time
Today, 05:55
Joined
Jun 8, 2006
Messages
29
shades

Thanks I found what you were identifying by adding a object on the spreadsheet . I was using the command button from the control tool box and it does not have that option available. Thanks for the help on this.

Tony
 

Users who are viewing this thread

Top Bottom