Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

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

  1. #1
    New Member
    Join Date
    Mar 2002
    Posts
    33
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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

  2. #2
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Your code ran for me, as-is.

  3. #3
    New Member
    Join Date
    Mar 2002
    Posts
    33
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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?

  4. #4
    New Member
    Join Date
    Mar 2002
    Posts
    33
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Anyone?

  5. #5
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    I did paste it into a command button.

  6. #6
    MrExcel MVP Ivan F Moala's Avatar
    Join Date
    Feb 2002
    Location
    Auckland, New Zealand
    Posts
    4,209
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Try setting the commandbuttons setting
    [TakeFocus*******] = False


    Kind Regards,
    Ivan F Moala From the City of Sails

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •