Excel VBAstring评估

我想知道是否有可能使用一些VBA在一大堆5charstring中查找定义的模式? 我已经探索了“instr”函数,但是我不知道它是否会执行我所需要的任务,基本上是在string中查找模式,如下所示:

ABABA BCBCB ..... EFEFE 

或者第一个字符与第三个字符和第五个字符相同,第二个字符与第四个字符相同的任何模式。

任何帮助或指导将受到感谢。

亲切的问候

吉姆

你可以做到这一点没有VBA,它仍然是足够快:

 =IF(AND(MID("ABABA",1,1)=MID("ABABA",3,1),MID("ABABA",2,1)=MID("ABABA",4,1),MID("ABABA",3,1)=MID("ABABA",5,1)),1,0) 

只需将“ABABA”replace为相应的单元格地址即可

把我的帽子扔在XD的戒指里

在这里输入图像说明

在这里输入图像说明

尝试Like运算符:

 Const testString = "ABABA" Dim myChar1 As String, myChar2 As String '// test 1/3/5 myChar1 = Mid(testString, 1, 1) '// test2/4 myChar2 = Mid(testString, 2, 1) If testString Like myChar1 & "[AZ]" & myChar1 & "[AZ]" & myChar1 Then MsgBox "Matches 1, 3 and 5" ElseIf testString Like "[AZ]" & myChar2 & "[AZ]" & myChar 2 & "[AZ]" Then Msgbox "Matches 2 and 4" End If 

或者使用Mid()函数:

 If Mid(testString, 1, 1) = Mid(testString, 3, 1) And _ Mid(testString, 3, 1) = Mid(testString, 5, 1) And _ Mid(testString, 1, 1) = Mid(testString, 5, 1) Then MsgBox "Matches 1, 3 and 5" ElseIf Mid(testString, 2, 1) = Mid(testString, 4, 1) Then MsgBox "Matches 2 and 4" End If 

检查这两个条件:

 Dim match1 As String, match2 As String Const testString As String = "ABABA" match1 = Left(testString, 1) & "[AZ]" & Left(testString, 1) & "[AZ]" & Left(testString, 1) match2 = "[AZ]" & Left(testString, 1) & "[AZ]" & Left(testString, 1) & "[AZ]" If testString Like match1 Or testString Like match2 Then MsgBox "findwindow likes it when anything matches" End If 

轮到我了:

 Function findPattern(inputStr As String) As Variant() Dim arr() As String Dim i As Integer arr = Split(inputStr) ReDim arr2(UBound(arr)) As Variant For i = LBound(arr) To UBound(arr) If Left(arr(i), 1) = Mid(arr(i), 3, 1) And _ Left(arr(i), 1) = Mid(arr(i), 5, 1) And _ Mid(arr(i), 2, 1) = Mid(arr(i), 4, 1) Then arr2(i) = "True" Else arr2(i) = "False" End If findPattern = arr2 Next End Function Sub trying() Dim t As String t = "ABABA BCBCB IOITU" arr = findPattern(t) 'returns an array {True,True,False} For x = 0 To 2 Debug.Print arr(x) Next End Sub 

这假设你在每个string中有多个单词。 它返回一个真正的假数组。

编辑

要find它,有任何模式使用这个UDF:

 Function findPattern(inputStr As String) As String Dim i As Integer For i = 5 To 1 Step -1 If Asc(Mid(inputStr, i, 1)) > 5 Then inputStr = Replace(inputStr, Mid(inputStr, i, 1), i) End If findPattern = inputStr Next End Function 

它将返回“ABABA”12121

您可以将其粘贴到工作簿中的模块中,然后像公式一样使用它: =findPattern("A1")将其复制下来。 然后在列上sorting,它会将所有类似图案(11111)的图案放在一起(12345)。

那么你也可以过滤这个列上的任何你想要的模式。

好吧所以我最终还是去了这个… 字符串模式优秀

令人尴尬的简单,从字面上不能相信我不认为这是一个选项,而只是假设它应该在VBA中完成。 一个宝贵的教训!

不得不修改公式,因为我的初始模式标志(1,3,5和2,4)评估为大脂肪FALSE,所以我决定寻找1,3,5和5是什么。 正如我在我对@zedfoxus的评论中所提到的,这让我到了现在需要的地方,但是使用VBA来制作这个将会很棒。 我要审查所有的答案,所以感谢您抽出宝贵的时间来回答,我frickin'爱这个地方!

和平!