我一直在寻找检查单元格中的每个字符以查看其大写但UCASE将字母转换为大写但我希望能够检查。我不确定应该如何做“做”声明。
Function Italic(rng As Range) As Variant
Dim strng As String
Dim iEnd As Long, iIni As Long, strngLen As Long
strngLen = Len(rng.Value2)
iIni = 1
Do While iEnd <= strngLen
Do While UCase(Mid(rng.Value, iEnd, 1)) = Mid(rng.Value, iEnd, 1) And Not rng.Characters(iEnd, 1).Font.Italic
If iEnd = strngLen Then Exit Do
iEnd = iEnd + 1
Loop
If iEnd > iIni Then strng = strng & Mid(rng.Value2, iIni, iEnd - iIni) & "|"
iEnd = iEnd + 1
iIni = iEnd
Loop
If strng <> "" Then Italic = Split(Left(strng, Len(strng) - 1), "|")
End Function
我不确定我应该在那个“大写”中做什么?
答案 0 :(得分:5)
您将检查字符大写是否等于当前字符:
While UCase(rng.Characters(iEnd, 1).Text) = rng.Characters(iEnd, 1).Text And Not rng.Characters(iEnd, 1).Font.Italic
感谢@SiddharthRoth的帮助。
现在您已经显示了完整的代码,还有一个注释。您应该避免使用excel中使用的单词命名变量,子函数和函数。
因此,请将名称更改为Itlc
或其他内容:
Function Itlc(rng As Range) As Variant
Dim strng As String
Dim iEnd As Long, iIni As Long, strngLen As Long
strngLen = Len(rng.Value2)
iIni = 1
Do While iEnd <= strngLen
Do While UCase(rng.Characters(iEnd, 1).Text) = rng.Characters(iEnd, 1).Text And Not rng.Characters(iEnd, 1).Font.Italic
If iEnd = strngLen Then Exit Do
iEnd = iEnd + 1
Loop
If iEnd > iIni Then strng = strng & Mid(rng.Value2, iIni, iEnd - iIni) & "|"
iEnd = iEnd + 1
iIni = iEnd
Loop
If strng <> "" Then Itlc = Split(Left(strng, Len(strng) - 1), "|")
End Function
答案 1 :(得分:3)
就这么简单
Function IsUpper(ByVal s) As Boolean
IsUpper = UCase(s) = s
End Function
这里的想法是创建字符串的副本并将副本转换为大写。如果输入参数字符串确实以大写字母开头,那么转换后的副本现在应该与输入完全相同。
如果您更改了Excel VBA的默认值以将文本值与Option Compare Text
进行比较,则将需要其他解决方案。但是这种情况很少见。上面的内容在绝大多数情况下都可以在Excel VBA中工作。
以下基于RegEx的功能是一种替代方法,即使在Option Compare Text
下也可以使用:
Function IsUpper(s) As Boolean
With CreateObject("VBScript.RegExp")
.Pattern = "^[^a-z]*$"
IsUpper = .Test(s)
End With
End Function
第二个功能也将在Access VBA中工作,该默认值是另一个字符串比较选项。
答案 2 :(得分:2)
您可以使用Regexp
Function OpalsOut(StrIn As String) As Variant
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "[A-Z]+"
.Global = True
.ignorecase = False
If .test(StrIn) Then
Set OpalsOut = .Execute(StrIn)
Else
OpalsOut = "No match"""
End If
End With
End Function
答案 3 :(得分:0)
Function CheckUppercase(inpt)
For i = 0 To Len(inpt) - 1
nwSt = Left$(Right(inpt, Len(inpt) - i), 1)
Select Case nwSt
Case " ", ",", ".", ";"
uUCase = 1
Case Else
If nwSt Like "*[A-Z]*" Then uUCase = 1 Else uUCase = 0: Exit For
End Select
Next
If uUCase = 0 Then CheckUppercase = 0 Else CheckUppercase = 1
' 0 means there is lowercase inside
End Function
答案 4 :(得分:0)
Function IsUpperInString(sInput As String) As Boolean
Dim sAplhabetAZ As String
Dim sCurrChar As String
Dim iLengthInput As Integer
Dim bResult As Boolean
On Error GoTo IsUpperInStringERROR
sAplhabetAZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
bResult = False
iLengthInput = Len(sInput)
If (iLengthInput = 0) Then
IsUpperInString = bResult
Exit Function
End If
For iCounter = 1 To iLengthInput
sCurrChar = Mid(sInput, iCounter, 1)
If (Instr(1, sAplhabetAZ, sCurrChar, vbBinaryCompare) > 0) Then
bResult = True
GoTo ExitThisFunction
End If
Next iCounter
ExitThisFunction:
IsUpperInString = bResult
Exit Function
IsUpperInStringERROR:
MsgBox "Function IsUpperInStringERROR: " & Str(Err.Number) & " - " & Err.Description
GoTo ExitThisFunction
End Function
答案 5 :(得分:0)
Function isUpperCase(parString As String) As Boolean
Dim value As Integer
value = Asc(Left(parString, 1))
isUpperCase = (value >= 65 And value <= 90) Or _
(value >= 192 And value <= 214) Or _
(value >= 216 And value <= 223) Or _
(value = 128) Or _
(value = 138) Or _
(value = 140) Or _
(value = 142) Or _
(value = 154) Or _
(value = 156) Or _
(value >= 158 And value <= 159) Or _
(value = 163) Or _
(value = 165)
End Function 'isUpperCase
答案 6 :(得分:0)
Function isAllUppercase(parString As String) As Boolean
Dim lenParString As Long
Dim pos As Long
Dim temp As String
lenParString = Len(parString)
isAllUppercase = True
For pos = 1 To lenParString
temp = Mid(parString, pos, 1)
If (isAlpha(temp) And isUpperCase(temp) = False) Then
isAllUppercase = False
Exit Function
End If
Next pos
End Function 'isAllUppercase
Function isAlpha(parString As String) As Boolean
Dim lenParString As Integer
Dim pos As Integer
lenParString = Len(parString)
For pos = 1 To lenParString
Select Case Asc(Mid(parString, pos, 1))
Case 65 To 90, _
97 To 122, _
138, _
140, _
142, _
154, _
156, _
158 To 159, _
192 To 214, _
216 To 246, _
249 To 255
' 65-90=A-Z, 97-122=a-z
' 138=Š, 140=Œ, 142=Ž, 154=š, 156=œ, 158=ž, 159=Ÿ, 192-214=À-Ö, 216-246=Ø-ö, 248-255=ø-ÿ
isAlpha = True
Case Else
isAlpha = False
Exit For
End Select
Next pos
End Function 'isAlpha
Function isUpperCase(parString As String) As Boolean
Select Case Asc(Left(parString, 1))
Case 65 To 90, _
138, _
140, _
142, _
159, _
192 To 214, _
216 To 222
' 65-90=A-Z
' 138=Š, 140=Œ, 142=Ž, 159=Ÿ, 192-214=À-Ö, 216-222=Ø-Þ
isUpperCase = True
Case Else
isUpperCase = False
End Select
End Function 'isUpperCase