首页 > 解决方案 > 验证 Excel VBA 中的文本框

问题描述

我有一个包含多个文本框的表单。我使用以下过程来验证活动文本框是否没有留空。如果是这样,那么文本框将重置为值 0 并且它可以工作

子 UFDataEntryCheckValue()

With UF_DataEntry.ActiveControl
    If Not IsNumeric(.Value) And .Value <> vbNullString Or UF_DataEntry.ActiveControl.Value = vbNullString Then
        MsgBox "Input must be a number and can not be blank"
       UF_DataEntry.ActiveControl.Value = 0

    End If
End With 

但是,有一个文本框需要重置为 36 的值,当我将以下语句添加到上述过程中时,出现错误

Dim Ctrl As Control

With UF_DataEntry.ActiveControl
    If Not IsNumeric(.Value) And .Value <> vbNullString Or UF_DataEntry.ActiveControl.Value = vbNullString Then
        MsgBox "Input must be a number and can not be blank"
        '.Value = vbNullString
        If Ctrl.Name = "DE_Text_Term" Then
            Ctrl.Value = 36
                Else: UF_DataEntry.ActiveControl.Value = 0
        End If
    End If
End With

标签: validation

解决方案


推荐阅读