Why won't this work? (see attached code)

Wingz

New Member
Joined
Mar 5, 2002
Messages
33
I recorded this macro and made a few minor modifications. When I run it from the "run macro" option, it works like a charm. However, when I paste this code into a commad button, clickling the button gives me a Run Time 1004 (Select method of Range class failed). This code is assuming the user has an entire row selected. Any help or advice?

Private Sub CommandButton1_Click()

Dim ans As String
Application.ScreenUpdating = False

ans = MsgBox("Are you sure you want to delete this employee?", vbYesNo, "Confirm Employee Deletion")


If ans = vbYes Then
Selection.Cut
Sheets("Deleted Employees").Select
Rows("2:2").Select
Selection.Insert Shift:=xlDown
Range("I2").Select
ActiveCell.Value = Format(Now, "mm/dd/yy")
Sheets("Current Employees").Select
Selection.Delete Shift:=xlUp
Else
End If


End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I know the code will run as is... it's when I paste it to a command button and run it from there that the problem arises. Am I missing syntax?
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top