简体   繁体   English

Excel VBA:输入特定密码后删除行

[英]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. 在VBA中,我已经准备好删除与其相关行的预订号的代码。

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. 假设预订号码:1800123被取消,需要从列表中删除。 I hit the button: Remove (btn_verwijderen) and I will get the first MsgBox. 我按下按钮:删除(btn_verwijderen),我将得到第一个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. Password功能将向您的子级返回TRUE (可以删除)或FALSE (不能删除)。 Therefore, your delete test needs to be based around the output of this function, which is illustrated in Sub Example . 因此,你删除测试需要基于解决此功能,其在示出的输出Sub Example

The Function Password output possibilities are as follows: 功能Password输出可能性如下:

  • TRUE : If booking number to delete does not exist in the array var (no password required) TRUE :如果数组var 中不存在要删除的预订号(不需要密码)
  • TRUE : If booking number to delete does exist in the array and the correct password is entered TRUE :如果阵列中确实存在要删除的预订号码, 并且输入了正确的密码
  • FALSE : If booking number to delete does exist in the array and the wrong password is entered FALSE :如果阵列中确实存在要删除的预订号, 并且输入了错误的密码

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 大部分工作将围绕此函数的结果构建您的delete语句

Notice I am passing the booking number 2 into the function manually. 注意,我正在将预订号2手动传递给该功能。 You need to swap the 2 for your variable booking number. 您需要将2交换为variable预订号。

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. 具有VBA知识的任何人都可以查看使用此方法存储的所有密码。 You can tighten the security by password protecting your VBA Project , however, this is still not 100% dummy proof. 您可以通过密码保护VBA Project来加强安全性,但是,这仍然不是100%的虚拟证明。 There are ways, which can be found on this site, that go into great detail about how to break these passwords. 在此站点上可以找到很多方法,它们详细介绍了如何破解这些密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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