简体   繁体   中英

Excel VBA: delete row after specific password is entered

I have a sheet filled with booking numbers and associated data. In VBA I already have the code ready to delete a booking number with its associated row.

Some of these booking numbers shouldn't be allowed to be removed and I would like a password (tied to this booking) to be used in order to remove it.

Here is the delete code I have:

Private Sub btn_verwijderen_Click()

' try to retrieve the product by ID
Dim rngIdList As Range, rngId As Range
Set rngIdList = ActiveSheet.Range([B2], [B2].End(xlDown))

Set rngId = rngIdList.Find(Me.txtBookingnr, LookIn:=xlValues)
If rngId Is Nothing Then
    ' bookingnumber is not found
    Exit Sub
Else
If MsgBox("you are about to delete: " & "Booking:" & Me.txtBookingnr & ". Are you sure?", vbYesNo) = vbYes Then
    If MsgBox("You can't undo this process. Sure to delete?", vbYesNo) = vbYes Then
        Sheets("Invoer").Range("Tabel133").Find(Me.txtBookingnr.Value).Delete
        Sheets("Gastenlijst_vertrekkers").Columns(2).Find(Me.txtBookingnr, , , , 1).EntireRow.Delete
        Sheets("Gastenlijst").Columns(2).Find(Me.txtBookingnr, , , , 1).EntireRow.Delete


    MsgBox "Booking is deleted. Refresh update now (automatically)"
    Call updatePlanning_Click
    Call btn_cancel_Click

    Else
        MsgBox "Nothing changed"
    End If
End If
End If
End Sub

Now, this code works for the rows that the user IS allowed to delete. But for the numbers the user CANNOT delete, should be protected by a password they need to fill in, in order to officially delete the row.

So for example:

Column B = Booking#
Column C = Initials
Column D = Surname
Column E = Checkin date
Column F = Checkout date
etc. etc. etc.

Let's say that Booking number: 1800123 is canceled and need to be removed from the list. I hit the button: Remove (btn_verwijderen) and I will get the first MsgBox. After hitting YES, there should be a new message asking for a password. After entering the correct password, the booking should be removed. BUT the password is tied to a specific booking.

Hopefully, some of you guys know how to achieve this. Thanks in advance for your help.

Here is some pseudo code that illustrates one approach. In your main sub, you need to state what booking you want to delete. You then need to check to see if that booking requires a password.

The function Password will return either TRUE (okay to delete) or FALSE (not okay to delete) to your sub. Therefore, your delete test needs to be based around the output of this function, which is illustrated in Sub Example .

The Function Password output possibilities are as follows:

  • TRUE : If booking number to delete does not exist in the array var (no password required)
  • TRUE : If booking number to delete does exist in the array and the correct password is entered
  • FALSE : If booking number to delete does exist in the array and the wrong password is entered

I recomend you test this code out as is to understand how it works. Then build it into your main sub. You should really only need to modify the array values in the function. The bulk of the work will be building your delete statements around the result of this function

Notice I am passing the booking number 2 into the function manually. You need to swap the 2 for your variable booking number.

Option Explicit

Sub Example()

If Password(2) Then                 'If function returns TRUE
    MsgBox "Add Delete Code Here"
Else                                'If function returns FALSE
    MsgBox "Incorrect Password"
End If

End Sub

Private Function Password(Booking As Long) As Boolean

Dim var As Variant, i As Long, PW As String

var = [{1, 2, 3, 4, 5; "Password1", "Password2", "Password3", "Password4", "Password5"}]

For i = LBound(var, 1) To UBound(var, 2)
    If Booking = var(1, i) Then
        PW = Application.InputBox("Password required to delete booking: " & Booking, "Password Protected", Type:=2)
            If PW = var(2, i) Then
                Password = True
                Exit Function
            Else
                Password = False
                Exit Function
            End If
    End If
Next i

Password = True

End Function

Disclaimer

Anyone with VBA knowledge will be able to view any passwords you store using this method. You can tighten the security by password protecting your VBA Project , however, this is still not 100% dummy proof. There are ways, which can be found on this site, that go into great detail about how to break these passwords.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM