如何让 Excel 用户表单验证然后编辑用户表单

How to get Excel Userform to Validate then edit Userform

我已经创建了一个用户表单,我试图让它在继续将数据输入到工作表之前验证并检查是否已输入所有字段。到目前为止,我已经获得了检查字段并在其中一个字段没有数据时显示错误消息的代码。

我尝试使用 Call 函数循环验证,首先到 Data_Validation,然后到 AddName_Click。他们都没有工作。

初始化用户窗体后,代码将转到以下子例程

Private Sub AddName_Click()


'Variable Declaration
Dim BlnVal As Boolean

'Find Last Row on Staff Data Worksheet

Dim LastRow As Long
    Dim rng As Range

   'Use a range on the sheet
    Set rng = Sheets("Staff Data").Range("A2:E900")

    ' Find the last row
    LastRow = Last(1, rng)


     'Data Validation
    Call Data_Validation


    'Find Area value
    If ARLArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "ARL"
    If LSQArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "LSQ"
    If KNBArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "KNB"
    If RSQArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "RSQ"
    If RevenueControlInspectors = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "RCI"
    If SpecialRequirementTeam = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "SRT"

    rng.Parent.Cells(LastRow + 1, 2).Value = EmployeeNo1.Value
    rng.Parent.Cells(LastRow + 1, 3).Value = FirstName1.Value
    rng.Parent.Cells(LastRow + 1, 4).Value = LastName1.Value

    'Find Grade value
    If CSA2 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSA2"
    If CSA1 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSA1"
    If CSS2 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSS2"
    If CSS1 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSS1"
    If CSM2 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSM2"
    If CSM1 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSM1"
    If AM = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "AM"
    If RCI = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "RCI"
    If SRT = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "SRT"

  On Error GoTo ErrOccured
    'Boolean Value
    BlnVal = 0





ErrOccured:
    'TurnOn screen updating
    Application.ScreenUpdating = True
    Application.EnableEvents = True

    'Empty Area
        ARLArea = False
        LSQArea = False
        KNBArea = False
        RSQArea = False
        RevenueControlInspectors = False
        SpecialRequirementTeam = False

    'Empty EmployeeNo1
        EmployeeNo1.Value = ""

    'Empty FirstName1
        FirstName1.Value = ""

    'Empty LastName1
        LastName1.Value = ""

    'Empty Grade
        CSA2 = False
        CSA1 = False
        CSS2 = False
        CSS1 = False
        CSM2 = False
        CSM1 = False
        AM = False
        RCI = False
        SRT = False

End Sub

如您所见,我已经添加了上面例程的其余部分,因为它应该转到数据验证例程以检查是否已输入所有数据。数据验证例程如下所示。

Sub Data_Validation()
' Check if all data has been entered on the userform

     If ARLArea = False And KNBArea = False And LSQArea = False And RSQArea = False And RevenueControlInspectors = False And SpecialRequirementTeam = False Then
        MsgBox "Select Area!", vbInformation, ("Area")
        ARLArea.SetFocus
        Exit Sub
        End If
     If EmployeeNo1 = "" Then
        MsgBox "Enter Employee Number!", vbInformation, ("Employee Number")
        EmployeeNo1.SetFocus
        Exit Sub
        End If
     If FirstName1 = "" Then
        MsgBox "Enter First Name!", vbInformation, ("First Name")
        FirstName1.SetFocus
        Exit Sub
        End If
     If LastName1 = "" Then
        MsgBox "Enter Last Name!", vbInformation, ("Last Name")
        LastName1.SetFocus
        Exit Sub
        End If
     If CSA2 = False And CSA1 = False And CSS2 = False And CSS1 = False And CSM2 = False And CSM1 = False And AM = False And RCI = False And SRT = False Then
        MsgBox "Select Grade!", vbInformation, ("Grade")
        CSA2.SetFocus
        Exit Sub
        End If

        BlnVal = 1

End Sub

我的问题是消息出现后,我单击“确定”。程序继续 运行 并将现有数据输入工作表。我想要它做的是当出现错误消息并单击“确定”时,用户窗体再次变为活动状态并且可以使用丢失的数据进行编辑。然后我希望它再次验证表单,直到输入所有字段,然后再将数据传输到工作表。

之所以继续,是因为当您退出这个子程序时,它只是结束了对当前子程序的处理,而不是附加代码。

您需要:

  1. 在下层子中引发错误并在上层子中处理异常
  2. 将此 Data_Validation() 转换为一个函数,该函数 return 是一个值,例如没有错误时为 0 或存在错误时为 1
  3. 只需将这一大块 if 移动到您用来触发插入的 On_Click 事件。如果您将代码移至主子,EXIT SUB 将在触发后正确踢出您的代码。然后将该值输入您的上层子。

最容易立即实施的方法是将 Data_Validation() 转换为一个函数并 return 设置一个值,如果验证完成则为 True 或 False。

如果验证失败,我们将处理错误消息和 return 主子程序的 FALSE 值以退出子程序,然后允许用户更新表单并再次单击按钮。我不确定你的 blnVal 是干什么用的。可能会尝试执行我已更新您的代码来执行的操作?-但使该特定版本的逻辑起作用的唯一方法是将变量设置为 public 并且这样做不被认为是好的做法。

请记住,如果您希望用户能够在代码处理过程中更新数据,这实际上是不可行的。您 可以 在弹出窗口中创建输入框而不是错误框,允许用户输入这些字段并在输入值并接受输入时继续处理代码。

Private Sub AddName_Click()


'Variable Declaration
Dim BlnVal As Boolean

'Find Last Row on Staff Data Worksheet

Dim LastRow As Long
    Dim rng As Range

   'Use a range on the sheet
    Set rng = Sheets("Staff Data").Range("A2:E900")

    ' Find the last row
    LastRow = Last(1, rng)


     'Data Validation - returns FALSE if failed, True if success
    If Data_Validation() = False Then
        Exit Sub
    End If


    'Find Area value
    If ARLArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "ARL"
    If LSQArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "LSQ"
    If KNBArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "KNB"
    If RSQArea = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "RSQ"
    If RevenueControlInspectors = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "RCI"
    If SpecialRequirementTeam = True Then rng.Parent.Cells(LastRow + 1, 1).Value = "SRT"

    rng.Parent.Cells(LastRow + 1, 2).Value = EmployeeNo1.Value
    rng.Parent.Cells(LastRow + 1, 3).Value = FirstName1.Value
    rng.Parent.Cells(LastRow + 1, 4).Value = LastName1.Value

    'Find Grade value
    If CSA2 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSA2"
    If CSA1 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSA1"
    If CSS2 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSS2"
    If CSS1 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSS1"
    If CSM2 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSM2"
    If CSM1 = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "CSM1"
    If AM = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "AM"
    If RCI = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "RCI"
    If SRT = True Then rng.Parent.Cells(LastRow + 1, 5).Value = "SRT"

  On Error GoTo ErrOccured
    'Boolean Value
    BlnVal = 0





ErrOccured:
    'TurnOn screen updating
    Application.ScreenUpdating = True
    Application.EnableEvents = True

    'Empty Area
        ARLArea = False
        LSQArea = False
        KNBArea = False
        RSQArea = False
        RevenueControlInspectors = False
        SpecialRequirementTeam = False

    'Empty EmployeeNo1
        EmployeeNo1.Value = ""

    'Empty FirstName1
        FirstName1.Value = ""

    'Empty LastName1
        LastName1.Value = ""

    'Empty Grade
        CSA2 = False
        CSA1 = False
        CSS2 = False
        CSS1 = False
        CSM2 = False
        CSM1 = False
        AM = False
        RCI = False
        SRT = False

End Sub

-

Function Data_Validation() As Boolean 'Declare Function with Bool as data type

'Default True. False if any conditions met. When a function is called, a new variable,
'with the function name and datatype given is created.  You'll set the value in the
'function.  When the function ends either in Exit Function or
'End Function, whatever is contained in this variable is returned as the Functions result
    Data_Validation = True
' Check if all data has been entered on the userform



     If ARLArea = False And KNBArea = False And LSQArea = False And RSQArea = False And RevenueControlInspectors = False And SpecialRequirementTeam = False Then
        MsgBox "Select Area!", vbInformation, ("Area")
        ARLArea.SetFocus
        Data_Validation = False
        Exit Function
        End If
     If EmployeeNo1 = "" Then
        MsgBox "Enter Employee Number!", vbInformation, ("Employee Number")
        EmployeeNo1.SetFocus
        Data_Validation = False
        Exit Function
        End If
     If FirstName1 = "" Then
        MsgBox "Enter First Name!", vbInformation, ("First Name")
        FirstName1.SetFocus
        Data_Validation = False
        Exit Function
        End If
     If LastName1 = "" Then
        MsgBox "Enter Last Name!", vbInformation, ("Last Name")
        LastName1.SetFocus
        Data_Validation = False
        Exit Function
        End If
     If CSA2 = False And CSA1 = False And CSS2 = False And CSS1 = False And CSM2 = False And CSM1 = False And AM = False And RCI = False And SRT = False Then
        MsgBox "Select Grade!", vbInformation, ("Grade")
        CSA2.SetFocus
        Data_Validation = False
        Exit Function
        End If

        BlnVal = 1


End Function