我正在尝试编写一个宏来查看列中的单元格是仅由辅音组成还是仅由元音组成,如果它们用黄色对整个行进行着色。我有这两个功能来检查辅音的数量:
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,谢谢你提前!
答案 0 :(得分:3)
你看到的问题是否与它有关,突出显示它不应该的行?这是因为你的函数应以
结尾 ConsonantCount = Kcount
和
VowelCount = Kcount
(而不是' cons = Kcount'和' vowl = Kcount')。
在VBA中,您使用函数的名称来返回值。
答案 1 :(得分:2)
没有必要逐字检查。要查看单元格是否包含所有辅音或元音,您可以执行类似(函数返回TRUE
或FALSE
)
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
答案 2 :(得分:1)
要查看单元格是仅包含元音还是仅包含辅音,您可以根据元音或辅音计数检查单元格文本的长度。例如,要查看单元格是否只是辅音,您可以这样做:
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