【问题标题】:Validate 3 Alpha- 3 Numeric- 1Alpha验证 3 Alpha - 3 数字 - 1Alpha
【发布时间】:2017-01-27 09:06:48
【问题描述】:

我需要在 Excel 中进行某种验证方面的帮助。我用的是office 2013。

用户将以下字符串捕获为 3 个 Alpha、3-Numeric、1 个 Alpha

BFE234G
DFR321F
DFE234F
RED1234
RED123F

我想在 VBA 或公式中进行验证,以向我展示捕获过程中的所有错误。以上错误(RED1234)

【问题讨论】:

    标签: excel excel-formula vba


    【解决方案1】:

    将此函数添加到 vba 模块中......

    Function IsValid(ByVal strVal As String) As Boolean
        Dim blnValid As Boolean
    
        If Len(strVal) <> 7 Then
            IsValid = False
            Exit Function
        End If
    
        If IsNumeric(Mid(strVal, 4, 3)) = False Then
            IsValid = False
            Exit Function
        End If
    
        If Left(strVal, 3) Like WorksheetFunction.Rept("[a-zA-Z]", 3) = False Then
            IsValid = False
            Exit Function
        End If
    
        If Right(strVal, 1) Like WorksheetFunction.Rept("[a-zA-Z]", 1) = False Then
            IsValid = False
            Exit Function
        End If
    
        IsValid = True
    End Function
    

    然后在单元格“=IsValid(B1)”等中使用函数......

    【讨论】:

      【解决方案2】:

      使用一个简单的函数,例如:

      Function IsValid(ByVal strVal As String) As Boolean
         IsValid = (strVal Like "[a-zA-Z][a-zA-Z][a-zA-Z][0-9][0-9][0-9][a-zA-Z]")
      End Function
      

      编辑根据@Slai的评论

      Function IsValid(ByVal strVal As String) As Boolean
         IsValid = (strVal Like "[a-zA-Z][a-zA-Z][a-zA-Z]###[a-zA-Z]")
      End Function
      

      这将允许大小写混合。如果要确保字母为大写,请将[a-zA-Z] 元素更改为[A-Z]

      【讨论】:

      • #[0-9] 的缩写
      【解决方案3】:

      如果用户不启用宏,则 Excel 公式替代方案:

      =SUMPRODUCT((FIND(MID(A1,ROW(1:8),1),"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")>10)*10^ROW(1:8))=10001110
      

      如果末尾有任何数字,上面将返回 TRUE,对于字符串中未找到的任何字符,则返回 #VALUE! 错误,因此您可以使用数据验证或 IFERRORLEN 来处理它功能:

      =(LEN(A1)=7)*IFERROR(SUMPRODUCT((FIND(MID(A1,ROW(1:8),1), 
                           "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")>10)*10^ROW(1:8)),)=10001110
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        相关资源
        最近更新 更多