如何使VBA函数作为布尔值?

时间:2016-05-19 14:51:21

标签: excel vba excel-vba

我已经给出了一个名单。例如:

Hanna-liis
Hanna-maria
Hanna-liina
Helgi-maie
Helju-mai

然后我有这个名字用符号H ??? - m ???给出。 我必须创建一个以boolean方式工作的函数,如果单词匹配symbol-true或false中的单词,函数必须回答。

例如Helgi-maie匹配H ??? - m ???但是Helju-mai没有。所以Helgi-maie是真的,helju-mai是假的。

我想出了这个

Function võrdlus(sõna, mask) As Boolean 
    võrdlus=true or false 
    If Left("A4", 1) = Left("B1", 1) And Left("A4", 7) = Left("B1", 7) Then
        võrdlus = True 
    Else 
        võrdlus = False 
    End If

这就是我现在所拥有的一切。但它告诉我所有答案都是正确的。

5 个答案:

答案 0 :(得分:0)

如果您研究正则表达式,生活将变得更容易...在您的项目中添加对Microsoft VBScript正则表达式5.5的引用,您可以使用这样的代码

Private Function checkByRegex(byVal strPattern, byVal strInput)       
    Dim regEx As New RegExp

    If strPattern <> "" Then
        With regEx
            .Global = True
            .MultiLine = True
            .IgnoreCase = False
            .Pattern = strPattern
        End With

        If regEx.Test(strInput) Then
            checkByRegex = True
        Else
            checkBYRegex = False
        End If
    End If
End Function

并以checkByRegex("H???-m???","Helju-maie")为例进行调用

答案 1 :(得分:0)

进一步澄清Dave的回复,根据regEx结果创建一个真/假的函数,并从子程序中调用它,你可以绑定到一个按钮或菜单选项:

Private Sub CheckName()
    checkByRegEx(strPattern, StrInput)
End Sub

Private Function checkByRegex(ByVal strPattern As string, ByVal strInput As String) AS Boolean
    Dim regEx         As New RegExp

    If strPattern <> "" Then
          With regEx
              .Global = True
              .MultiLine = True
              .IgnoreCase = False
              .Pattern = strPattern
          End With

          checkByRegEx = regEx.Test(strInput)
    End If
End Function

答案 2 :(得分:0)

Sub võrdlus()
    Dim checkFormat As String

    checkFormat = Range("A4").Value
    For i = 1 To Range("B1", Range("B1").End(xlDown)).Rows.Count
        checkValue = Range("B" & i).Value
        If checkValue Like checkFormat And Len(checkValue) = 10 Then
            Range("C" & i).Value = True
        Else
            Range("C" & i).Value = False
        End If
    Next i
End Sub

根据值的位置,您需要调整范围,但这样可以。

答案 3 :(得分:-1)

我很确定你可以使用公式而不是VBA函数:

=IFERROR(IF(AND(SEARCH(A4,B1)<>0,LEN(A4)=LEN(B1)),"TRUE","FALSE"),"FALSE")

其中A4是名称的符号版本,B1是全名。

答案 4 :(得分:-1)

不确定你想要做什么,但这是我的版本:

               'Once pressed, loop through the entire list and check is criteria is met
            'return TRUE or FALSE when word starts with an H and has a length of 5 AND
            'the second word starts with m with a length of 4
            Sub Button1_Click()
                For i = 1 To Sheet1.Range("C1").Value
                    Sheet1.Cells(i, 2).Value = checkWord(Sheet1.Cells(i, 1).Value)
                Next i
            End Sub

            'Chec if value evaluates to TRUE or FALSE
            Function checkWord(ByVal word As String) As Boolean
                Dim wordSplit As Variant ' An array where word is split up

                'Split word with delimiter "-"
                wordSplit = Split(word, "-")

                'The array now holds...
                'EXAMPLE:
                'wordSplit(0) = Helgi
                'wordSplit(1) = maie
                'len(Helgi) = 5
                'len(maie) = 4
                'FUNCTION should return TRUE

                'First word in array starts with a "H"?
                If InStr(1, wordSplit(0), "H", vbBinaryCompare) = 1 Then
                    'Does it have a length of 5?
                    If Len(wordSplit(0)) = 5 Then
                        'The first 2 criteria are correct,
                        'now check if 2nd word starts with a "m"
                        If InStr(1, wordSplit(1), "m", vbBinaryCompare) = 1 Then
                            'If true, does it have a length of 4?
                            If Len(wordSplit(1)) = 4 Then
                                checkWord = "TRUE"
                            Else 'No it doesnt
                                checkWord = "FALSE"
                            End If
                        'No it does not start with "m"
                        Else
                            checkWord = "FALSE"
                        End If
                    'Not it is not 5 letters long
                    Else
                        checkWord = "FALSE"
                    End If
                End If
            End Function

enter image description here