如何在字符串中的第二个元音后找到该字符

时间:2016-05-02 08:25:36

标签: excel excel-vba excel-formula vba

我认为这How to find the character after second vowel in a string in EXCEL?很有意思,除了过于宽泛。

另外我已经回答了,但为时已晚。

问题

例如,如果我的字符串是

  

快乐之歌

输出应为

  

页。

如果是空白,则显示“Nothing”

可能的解决方案

一种公式方法(礼貌 tikkaty

=IF(A1<>"",IFERROR(MID(A1,FIND("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a","|"),"e","|"),"i","|"),"o","|"),"u","|"),FIND("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a","|"),"e","|"),"i","|"),"o","|"),"u","|"))+1)+1,1),"no second vowel found"),"")

2 个答案:

答案 0 :(得分:3)

可能的VBA UDF解决方案:

测试代码

Sub test()
Debug.Print StrOut("The Happy Song")
Debug.Print StrOut("The Gypsy")
Debug.Print StrOut("1234")
End Sub

UDF

Function StrOut(strIn As String) As String
Dim objRegex As Object
Dim objRegexM As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "[aeiou].*?[aeiou](.)"
    If .test(strIn) Then
        Set objRegexM = .Execute(strIn)
        strOut = objRegexM(0).submatches(0)
    Else
        StrOut = "Nothing"
    End If
End With
End Function

答案 1 :(得分:1)

这是一个无正则表达式的变体:

Function charTest(s As String) As String
    Dim i As Integer, j As Integer, r As String
    For i = 1 To Len(s)
        If UCase(Mid(s, i, 1)) Like "[AEIOU]" Then j = j + 1
        If j >= 2 Then Exit For
    Next i

    If j >= 2 And i < Len(s) Then r = Mid(s, i + 1, 1)
    If r = "" Then r = "Nothing"

    charTest = r
End Function

验证

Sub test2()
    Debug.Print charTest("abcdefghijkl")
    Debug.Print charTest("Excellence")
    Debug.Print charTest("Animal")
    Debug.Print charTest("The guppy pond")
    Debug.Print charTest("The smallest bike")
    Debug.Print charTest("Shea")
End Sub
  

˚F
  升
  米
  p
  升
  没有