我有一个由辅音和元音组成的5char alpha字符串列表。 我试图识别每个字符串中的组合。
例如,DBNJE是CCCCV,AHOIS是VCVVC。
我已将此作为一系列5个相当笨拙的公式来编写,以评估字符串中每个字符的C或V属性,如下所示
Char1
=IF(OR(LEFT($B2,1)="A",LEFT($B2,1)="E",LEFT($B2,1)="I",LEFT($B2,1)="O",LEFT($B2,1)="U"),"V","C")
和
Char2
=IF(OR(MID($B2,2,1)="A",MID($B2,2,1)="E",MID($B2,2,1)="I",MID($B2,2,1)="O",MID($B2,2,1)="U"),"V","C")
Etc等
到目前为止我?
然后我在单个单元格中连接结果,给我组合,就像上面的第一个例子一样。
这很好用,但我想使用VBA自动化它。有人可以告诉我最好的方法吗?
提前致谢!
答案 0 :(得分:3)
我也会跳上这个潮流......这会检查每个角色的ASCII码值,忽略A-Z范围之外的任何东西以供考虑。
Public Function GetPattern(ByRef rng As Excel.Range)
Dim str As String ' the value being parsed
Dim ret As String ' the return value for the function
Dim i As Long
Const C As String = "C"
Const V As String = "V"
Dim char As String
str = Trim(UCase(rng.Value))
For i = 1 To Len(str)
char = Mid(str, i, 1)
'Ignores numbers and non-printing characters, symbols, etc.
If 65 <= Asc(char) And Asc(char) <= 90 Then
Select Case Asc(char)
Case 65, 69, 73, 79, 85 'Ascii codes for A, E, I, O, U
ret = ret & V
Case Else
ret = ret & C
End Select
End If
Next
GetPattern = ret
End Function
答案 1 :(得分:0)
将此功能插入模块并在单元格中输入&#34; =单词(&#34;参考&#34;),其中&#34; reference&#34;是一个包含您要处理的字符串之一的单元格。这也可以放在子程序中循环遍历所有字符串单元格,但是您需要提供更多信息。
Function Words(str as String) as String
Dim str as String
Dim result as string
Dim i as Integer
For i = 1 to Len(str)
If Mid(str,i,1) like [aeiouAEIOU] then
Result = "V" & Result
Else
Result = "C" & Result
End if
Words = Result
End Function
答案 2 :(得分:0)
这里没什么好看的。此代码将进入模块并从包含输出数据的单元格中引用。
Public Function ConsonantVowel(ByVal sInputString As String) As String
Dim sReturnData As String
Dim x As Integer
sReturnData = ""
x = 1
While x <= Len(sInputString)
If UCase(Mid(sInputString, x, 1)) = "A" Or UCase(Mid(sInputString, x, 1)) = "E" Or UCase(Mid(sInputString, x, 1)) = "I" _
Or UCase(Mid(sInputString, x, 1)) = "O" Or UCase(Mid(sInputString, x, 1)) = "U" Then
sReturnData = sReturnData & "V"
Else
sReturnData = sReturnData & "C"
End If
x = x + 1
Wend
ConsonantVowel = sReturnData
End Function
答案 3 :(得分:0)
我创建了一个用户定义的函数来循环遍历字符串中的字符,如果它是辅音则返回C,如果它是元音,则返回V.
将以下代码放在标准VBA模块中:
Function ConsonantOrVowel(strWord As String) As String
Dim nLen As Integer
nLen = Len(strWord)
ConsonantOrVowel = ""
For i = 1 To nLen
If UCase(Mid(strWord, i, 1)) Like "[AEIOU]" Then
ConsonantOrVowel = ConsonantOrVowel + "V"
Else
ConsonantOrVowel = ConsonantOrVowel + "C"
End If
Next i
End Function
接下来,在Excel单元格中,输入公式=ConsonantOrVowel(A1)
,就像输入常规公式一样。 (注意,前面的示例假定您要测试的字符串在单元格A1中)。
答案 4 :(得分:0)
这是一种利用正则表达式定义元音的方法
~
C
V
Option Explicit
Function VorC(S As String) As String
Dim RE As Object
Dim sTemp As String
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.ignorecase = True
.Pattern = "[AEIOU]"
End With
sTemp = RE.Replace(S, "~")
RE.Pattern = "[^~]"
sTemp = RE.Replace(sTemp, "C")
VorC = Replace(sTemp, "~", "V")
End Function
如果你想将偶然的Y
作为元音包括在内,并且很高兴在它被两个辅音包围的实例中称之为元音,那么可以将第一个Regex Replacement例程更改为:
With RE
.Global = True
.ignorecase = True
.Pattern = "[AEIOU]|([^AEIOU])Y([^AEIOU])"
End With
sTemp = RE.Replace(S, "$1~$2")
保持UDF的其余部分不变。
然而,调用Y
元音的规则比这更复杂,有时它不是真正的元音,而是双元音的一部分,所以事情很快就会模糊。