检查单元是否仅包含元音或辅音Vba

我试图写一个macros,看看列中的单元格是只包含辅音还是只包含元音,以及是否用黄色为整行着色。 我有这两个函数来检查辅音的数量:

Function ConsonantCount(cons As String) As Integer Dim Str Dim KCount As Integer Dim i As Integer Dim Chr As String Str = ActiveSheet.Range("A1").Value KCount = 0 For i = 1 To Len(Str) Chr = UCase(Mid(Str, i, 1)) If Not Chr Like "[AEIOU]" Then KCount = KCount + 1 End If Next i cons = KCount End Function 

各个元音:

 Function VowelCount(vowl As String) As Integer Dim Str Dim KCount As Integer Dim i As Integer Dim Chr As String Str = ActiveSheet.Range("A1").Value KCount = 0 For i = 1 To Len(Str) Chr = UCase(Mid(Str, i, 1)) If Chr Like "[AEIOU]" Then KCount = KCount + 1 End If Next i vowl = KCount End Function 

然后我使用函数来查看是否有两个不同的列M和N有0个辅音/元音:

 Dim iix As Long, FFX As Long With Sheets("JP") FFX = .Range("M" & .Rows.count).End(xlUp).Row For iix = 1 To FFX If ConsonantCount(.Range("M" & iix)) = 0 Then .Rows(iix).Interior.Color = vbYellow End If If ConsonantCount(.Range("N" & iix)) = 0 Then .Rows(iix).Interior.Color = vbYellow End If If VowelCount(.Range("M" & iix)) = 0 Then .Rows(iix).Interior.Color = vbYellow End If If VowelCount(.Range("N" & iix)) = 0 Then .Rows(iix).Interior.Color = vbYellow End If Next iix End With 

我真的需要一些指导,相当新的VBA,谢谢你提前!

要查看单元格是只包含元音还是仅包含辅音,您可以根据元音或辅音数量检查单元格文本的长度。 例如,要查看一个单元格是否只是辅音,你可以这样做:

 Len(.Range("M" & iix)) = ConsonantCount(.Range("M" & iix)) 

您还需要检查空单元格,以免突出显示。 牢记这些想法,我重新编写了一些代码,这样我的工作方式就可以实现。

 Function ConsonantCount(cons As String) As Integer 'Dim Str Dim KCount As Integer Dim i As Integer Dim Chr As String 'Str = ActiveSheet.Range("A1").Value KCount = 0 For i = 1 To Len(cons) Chr = UCase(Mid(cons, i, 1)) If Not Chr Like "[AEIOU]" Then KCount = KCount + 1 End If Next i ConsonantCount = KCount End Function Function VowelCount(vowl As String) As Integer 'Dim Str Dim KCount As Integer Dim i As Integer Dim Chr As String 'Str = ActiveSheet.Range("A1").Value KCount = 0 For i = 1 To Len(vowl) Chr = UCase(Mid(vowl, i, 1)) If Chr Like "[AEIOU]" Then KCount = KCount + 1 End If Next i VowelCount = KCount End Function Sub Test() Dim iix As Long Dim FFX As Long With Sheets("JP") FFX = .Range("M" & .Rows.Count).End(xlUp).Row For iix = 1 To FFX If Len(.Range("M" & iix)) > 0 Then If Len(.Range("M" & iix)) = ConsonantCount(.Range("M" & iix)) Or Len(.Range("M" & iix)) = VowelCount(.Range("M" & iix)) Then .Rows(iix).Interior.Color = vbYellow End If End If If Len(.Range("N" & iix)) > 0 Then If Len(.Range("N" & iix)) = ConsonantCount(.Range("N" & iix)) Or Len(.Range("N" & iix)) = VowelCount(.Range("N" & iix)) Then .Rows(iix).Interior.Color = vbYellow End If End If Next iix End With End Sub 

你看到的问题是与它突出显示的行不应该? 这是因为你的函数应该以

ConsonantCount = Kcount

VowelCount = Kcount

(而不是'cons = Kcount'和'vowl = Kcount')。

在VBA中,你使用函数的名字来返回一个值。

没有必要逐字检查。 要查看单元格是否包含所有辅音或元音,可以执行类似的操作(函数返回TRUEFALSE

 Option Explicit Option Compare Text 'case insensitive compares Function AllConsonants(R As Range) As Boolean Dim sPat As String sPat = WorksheetFunction.Rept("[bcdfghjklmnpqrstvwxyz]", Len(R.Text)) AllConsonants = R.Text Like sPat And Len(R.Text) > 0 End Function Function AllVowels(R As Range) As Boolean Dim sPat As String sPat = WorksheetFunction.Rept("[aeiou]", Len(R.Text)) AllVowels = R.Text Like sPat And Len(R.Text) > 0 End Function