Excel用户定义的函数 - 字段匹配特定条件

时间:2016-03-12 23:21:40

标签: excel vba

vba新手 - 我基本上尝试将这个非常长的if语句转换为用户定义的函数。该函数将检查该字段是否是2个字母,2个数字,下划线,1个字母,2个数字的正确格式。

=IF(AND(AND(CODE(MID(A2,1,1))>64,CODE(MID(A2,1,1))<91),AND(CODE(MID(A2,2,1))>64,CODE(MID(A2,2,1))<91),AND(CODE(MID(A2,3,1))>47,CODE(MID(A2,3,1))<57),AND(CODE(MID(A2,4,1))>47,CODE(MID(A2,4,1))<57),CODE(MID(A2,5,1))=95,AND(CODE(MID(A2,6,1))>64,CODE(MID(A2,6,1))<91),AND(CODE(MID(A2,7,1))>47,CODE(MID(A2,7,1))<57),AND(CODE(MID(A2,8,1))>47,CODE(MID(A2,8,1))<57),"Good","Bad Syntax")

A2的例子是AA01_A05

下面的代码 - 最后飞过它。任何帮助,将不胜感激。

Function bincheck(strValue As String) As Boolean

Dim AislePos As Integer, Rackno As Integer, Udr As Integer, ShelfPos As     Integer, BinNo As Integer
Dim TrueAisle As Boolean, TrueRack As Boolean, TrueUdr As Boolean, TrueShelf As Boolean, TrueBin As Boolean

For AislePos = 1 To 2
    Select Case Asc(Mid(strValue, AislePos, 1))
        Case 65 To 90
            TrueAisle = True
        Case Else
            TrueAisle = False
            Exit For
    End Select
Next
For Rackno = 3 To 4
    Select Case Asc(Mid(strValue, Rackno, 1))
        Case 48 To 56
            TrueRack = True
        Case Else
            TrueRack = False
            Exit For
    End Select
Next
 For Udr = 5 To 5
    Select Case Asc(Mid(strValue, Udr, 1))
        Case Is = 95
            TrueUdr = True
        Case Else
            TrueUdr = False
            Exit For
    End Select
Next
For ShelfPos = 6 To 6
    Select Case Asc(Mid(strValue, ShelfPos, 1))
        Case 65 To 90
            TrueShelf = True
        Case Else
            TrueShelf = False
            Exit For
    End Select
Next
 For BinNo = 7 To 8
    Select Case Asc(Mid(strValue, BinNo, 1))
        Case 48 To 56
            TrueBin = True
        Case Else
            TrueBin = False
            Exit For
    End Select
Next
Select Case bincheck
     Case TrueRack = True And TrueAisle = True And TrueUdr = True And TrueShelf = True And TrueBin = True
bincheck = "Good"
    Case Else
bincheck = "Bad"
End Select
End Function

3 个答案:

答案 0 :(得分:1)

我似乎是最后一个Select Case语句,

Select Case bincheck
     Case TrueRack = True And TrueAisle = True And TrueUdr = True And TrueShelf = True And TrueBin = True
bincheck = "Good"
    Case Else
bincheck = "Bad"
End Select

......会更好,

bincheck = TrueRack And TrueAisle And TrueUdr And TrueShelf And TrueBin

您无需检查布尔值是否等于为真。它已经是真或假。

检查传入函数的字符串的长度可能是最好的整体&#39;包装&#39;如果条件。如果不满足,那么没有一个布尔值被设置超过它们的默认声明值False(即当它们被声明为布尔值时,它们的生命开始时为False)。

Function bincheck(strValue As String) As Boolean

    Dim pos As Integer
    Dim TrueAisle As Boolean, TrueRack As Boolean, TrueUdr As Boolean, TrueShelf As Boolean, TrueBin As Boolean

    If Len(strValue) = 8 Then
        For pos = 1 To 2
            Select Case Asc(Mid(strValue, pos , 1))
                Case 65 To 90
                    TrueAisle = True
                Case Else
                    TrueAisle = False
                    Exit For
            End Select
        Next
        For pos = 3 To 4
            Select Case Asc(Mid(strValue, pos , 1))
                Case 48 To 56
                    TrueRack = True
                Case Else
                    TrueRack = False
                    Exit For
            End Select
        Next
         For pos = 5 To 5
            Select Case Asc(Mid(strValue, pos , 1))
                Case Is = 95
                    TrueUdr = True
                Case Else
                    TrueUdr = False
                    Exit For
            End Select
        Next
        For pos = 6 To 6
            Select Case Asc(Mid(strValue, pos , 1))
                Case 65 To 90
                    TrueShelf = True
                Case Else
                    TrueShelf = False
                    Exit For
            End Select
        Next
        For pos = 7 To 8
            Select Case Asc(Mid(strValue, pos , 1))
                Case 48 To 56
                    TrueBin = True
                Case Else
                    TrueBin = False
                    Exit For
            End Select
        Next
    End If

    bincheck = TrueRack And TrueAisle And TrueUdr And TrueShelf And TrueBin

End Function

答案 1 :(得分:1)

如果您希望函数返回“Good”或“Bad”等文本,那么第一行必须

Function bincheck(strValue As String) As Boolean

如果你想返回一个二进制值(如你的变量名所示)那么你可以保留第一行

True

但是必须调整末尾的变量bincheck以返回二进制值,例如Falsebincheck = True Case Else bincheck = False

bincheck

但是函数的名称为Function IsTheStringFormattedCorrectly(strValue As String) As Boolean ,这可能会令人恼火,我建议使用该函数的替代名称,例如:

True

然后,Falsedatamig1的返回值可能会更好理解。

答案 2 :(得分:1)

整件事可以简化为Like声明

Function bincheck(strValue As String) As Boolean
     bincheck = strValue LIKE "[A-Z][A-Z]##_[A-Z]##"
End Function