VBA to delete specified row

Will85

Board Regular
Joined
Apr 26, 2012
Messages
240
Office Version
  1. 365
Platform
  1. Windows
I do not trust my users to have access to this workbook without it being protected.

Users need to be able to delete rows on a protected sheet.

I want vba code that asks the user what account number they want to delete "What customer ID to delete?"

The vba box should accept a user input for a number, then go to SheetABC finds the matching user input in Column A (if no match is found then respond with a message saying "The account deosnt exist), unprotect the sheet, go to that specific row, delete the row in its entirety, then reprotect the sheet.

For exmaple

VBA msgbox
"What customer ID to delete?"

User input
206700

VBA goes to sheetABC, finds 206700 in column A row 419, unprotects the sheet, deletes row 419, reprotects the sheet.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this:

VBA Code:
Public Enum appInputBox
IBNumber = 1
End Enum
Sub Will85()
    Dim accNo As String, RowDelete As Range
    Dim ws As Worksheet: Set ws = Worksheets("sheetABC")
    
    accNo = Application.InputBox("What customer ID to delete?", Type:=IBNumber)
    Set RowDelete = ws.Range("A:A").Find(what:=accNo, LookIn:=xlValues, lookat:=xlWhole)
    If Not RowDelete Is Nothing Then
        ws.Unprotect Password:="123"
        RowDelete.EntireRow.Delete
        ws.Protect Password:="123"
        Else
        MsgBox "The account doesn't exist"
    End If
End Sub
 
Upvote 0
Try this:

VBA Code:
Public Enum appInputBox
IBNumber = 1
End Enum
Sub Will85()
    Dim accNo As String, RowDelete As Range
    Dim ws As Worksheet: Set ws = Worksheets("sheetABC")
   
    accNo = Application.InputBox("What customer ID to delete?", Type:=IBNumber)
    Set RowDelete = ws.Range("A:A").Find(what:=accNo, LookIn:=xlValues, lookat:=xlWhole)
    If Not RowDelete Is Nothing Then
        ws.Unprotect Password:="123"
        RowDelete.EntireRow.Delete
        ws.Protect Password:="123"
        Else
        MsgBox "The account doesn't exist"
    End If
End Sub
That worked perfectly. I have never seen Enum before. Thank you for teaching me something new, now I have to go and try and understand what its doing!
 
Upvote 0
That worked perfectly. I have never seen Enum before. Thank you for teaching me something new, now I have to go and try and understand what its doing!
Glad I could help, and thanks for the feedback ?
In short, it's forcing the user to input a number. Try inputting an alpha string (or mixed) and you'll see what I mean.
 
Upvote 0
Glad I could help, and thanks for the feedback ?
In short, it's forcing the user to input a number. Try inputting an alpha string (or mixed) and you'll see what I mean.
Ok, im glad you mentioned that, some of our account numbers have leading alpha characters, I just handt thought to test that just yet.

So an account number could be 5978 or it could be ABC5978

Thoughts?
 
Upvote 0
Ok, im glad you mentioned that, some of our account numbers have leading alpha characters, I just handt thought to test that just yet.

So an account number could be 5978 or it could be ABC5978

Thoughts?
Leave it with me, it'll need to change.
 
Upvote 0
Just lose the Enum

VBA Code:
Sub Will85_2()
    Dim accNo As String, RowDelete As Range
    Dim ws As Worksheet: Set ws = Worksheets("sheetABC")
    
    accNo = Application.InputBox("What customer ID to delete?")
    Set RowDelete = ws.Range("A:A").Find(what:=accNo, LookIn:=xlValues, lookat:=xlWhole)
    If Not RowDelete Is Nothing Then
        ws.Unprotect Password:="123"
        RowDelete.EntireRow.Delete
        ws.Protect Password:="123"
        Else
        MsgBox "The account doesn't exist"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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