1

I'm trying to make a button to delete just one row from a table on Excel using vba but when I press the button and select more than just one row or a lot of cells then deletes everything, I want to limit the selection from E to I (and no matters the number of row) Here is my code and thanks a lot, hope you can help me, I don't find anything similar yet.

Sub delete_a_row()
Dim variable As Range


 On Error GoTo local_err
     CONFIRM = MsgBox("do you wanna delete a row?", vbYesNo + vbExclamation, "delete row")
        If (CONFIRM = vbYes) Then

                Set variable = Application.InputBox("select a row", "delete row", , , , , , 8)
                Range(variable.Address).Select
                Selection.Delete Shift:=xlUp

        End If
local_err:
End Sub

I know looks kinda messy, sorry for that I'm new on VBA

2 Answers 2

1

Limited Row Selection

  • The following will delete the first row of the first range (if non-contiguous) of the selection from columns E to I.
  • What I mean by first range is if you select a range starting with row 6 and press CTRL and select another range starting with row 2, then row 6 will be deleted (See Range.Areas).

The Code

Option Explicit

Sub delete_a_row()
    Dim Confirm
    On Error GoTo local_err
    Confirm = MsgBox("do you wanna delete a row?", _
                     vbYesNo + vbExclamation, "delete row")
    If (Confirm = vbYes) Then
        Dim variable As Range
        On Error Resume Next
        Set variable = Application.InputBox("select a row", _
                                            "delete row", , , , , , 8)
        If Err.Number = 0 Then
            On Error GoTo local_err
            With variable
                Range(Cells(.Row, "E"), Cells(.Row, "I")).Delete Shift:=xlUp
            End With
        Else
            On Error GoTo local_err
            MsgBox "User cancelled."
        End If
    Else
        MsgBox "User selected No."
    End If
local_err:
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

MsgBox "User cancelled." after On Error GoTo local_err seems odd? Maybe they should be reversed?
0

If the only thing you want deleted is columns E through I then:

Sub ytrewq()
    Dim variable As Range, rw As Long

    Set variable = Application.InputBox("select a row", "delete row", , , , , , 8)
    rw = variable(1).Row

    Range(Cells(rw, "E"), Cells(rw, "I")).Delete Shift:=xlUp
End Sub

This code will remove cells from only a single row.

1 Comment

Is still letting me select more than E to I, If the user select (For example) "D to I" then the program will say "Hey dude you can't delete this, is exceding the range" but Thanks Gary, I can apply part of the code for make it better =D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.