Vab code Delete button

Sacruzsa

New Member
Joined
Feb 28, 2022
Messages
29
Office Version
  1. 2019
Platform
  1. Windows
Hi guys

I need your help for the following problem:

I have created a form for quick retrieval of phone numbers and email addresses, everything works except for the delete button
I want to protect this with a password so that it cannot be accidentally deleted, but there is something wrong with my code.
When I press the delete button it asks for a password and when I press cancel the field is deleted anyway, hopefully someone of you can help me

code:
Private Sub CommandButton5_Click()

Dim password As Variant

password = Application.InputBox("Please Enter Password", "Password Protected Macro")

Select Case password

Case Is = False
'do nothing

Case Is = "23BnMed"

Case Else

MsgBox "The password you entered was incorrect"

End Select

If Me.ListBox1.ListIndex >= 0 Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("AddressBook")

Dim myRow As Long
myRow = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0), sh.Range("A:A"), 0)

sh.Range("A" & myRow).EntireRow.Delete

Call Refresh_List_Box

MsgBox "Record has been deleted", vbInformation

End If

End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
End Sub after msgbox otherwise there is nothing to stop the rest of the code from running. I cannot see how the return value can ever be False, so it's really either the password value you want, or it's not. If it's provided but incorrect, your Case Else will come into play. If it's not provided the return value is a zls (zero length string) so it's still not. Might as well just use an IF block? I might write it thus (please use vba code tags for code - vba button on posting toolbar) to maintain indentation and improve readability:
VBA Code:
Private Sub CommandButton5_Click()
Dim password As Variant
Dim sh As Worksheet

If Not Application.InputBox("Please Enter Password", "Password Protected Macro") = "23BnMed" Then
     MsgBox "Incorrect password entered."
     Exit Sub
End If

Dim myRow As Long
Dim sh As Worksheet

If Me.ListBox1.ListIndex >= 0 Then
   Set sh = ThisWorkbook.Sheets("AddressBook")
   myRow = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0), sh.Range("A:A"), 0)
   sh.Range("A" & myRow).EntireRow.Delete
   Refresh_List_Box
   MsgBox "Record has been deleted", vbInformation
End If
Set sh = Nothing

End Sub
I would declare all variables rather than just some. IMO you should have Option Explicit at the top of every code module, and in fact, set that as an option going forward. If interested, find "require variable declaration" option and turn it on for new modules.
 
Upvote 0
Solution
End Sub after msgbox otherwise there is nothing to stop the rest of the code from running. I cannot see how the return value can ever be False, so it's really either the password value you want, or it's not. If it's provided but incorrect, your Case Else will come into play. If it's not provided the return value is a zls (zero length string) so it's still not. Might as well just use an IF block? I might write it thus (please use vba code tags for code - vba button on posting toolbar) to maintain indentation and improve readability:
VBA Code:
Private Sub CommandButton5_Click()
Dim password As Variant
Dim sh As Worksheet

If Not Application.InputBox("Please Enter Password", "Password Protected Macro") = "23BnMed" Then
     MsgBox "Incorrect password entered."
     Exit Sub
End If

Dim myRow As Long
Dim sh As Worksheet

If Me.ListBox1.ListIndex >= 0 Then
   Set sh = ThisWorkbook.Sheets("AddressBook")
   myRow = Application.WorksheetFunction.Match(Me.ListBox1.List(Me.ListBox1.ListIndex, 0), sh.Range("A:A"), 0)
   sh.Range("A" & myRow).EntireRow.Delete
   Refresh_List_Box
   MsgBox "Record has been deleted", vbInformation
End If
Set sh = Nothing

End Sub
I would declare all variables rather than just some. IMO you should have Option Explicit at the top of every code module, and in fact, set that as an option going forward. If interested, find "require variable declaration" option and turn it on for new modules.
Hi Micron,
thanks for the help, now it works perfectly
 
Upvote 0
You're welcome. Perhaps mark this one as solved then?
 
Upvote 0
FOR FUTURE READERS - in my first post I meant to write Exit Sub, not End Sub. Unfortunately I cannot revise now.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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