如何在单元格中find“小写字母”的第一个实例?

如何在Excel中使用VBA在单元格中查找“小写字符”的第一个实例?
我曾尝试使用ASCII值,但没有奏效。

试试下面的小UDF:

Public Function Findlower(rin As Range) As Long Dim v As String, CH As String, i As Long Findlower = 0 v = rin.Text L = Len(v) For i = 1 To L If Mid(v, i, 1) Like "[az]" Then Findlower = i Exit Function End If Next i End Function 

它将返回string中任何小写字母的第一个实例的位置:

在这里输入图像说明

您可以在UDF中使用RegExp来避免循环每个字符:

 Function FirstLower(strIn As String) as String Dim objRegex As Object Dim objRegM As Object Set objRegex = CreateObject("vbscript.regexp") With objRegex .Pattern = "[az]" .ignorecase = False If .test(strIn) Then Set objRegM = .Execute(strIn)(0) FirstLower = objRegM.firstindex + 1 Else FirstLower = "no match" End If End With End Function 

我想你想删除你的string中的小写字母的第一部分:

 Public Function DeletFirstLowerPart(strTemp As String) As String Dim strResult As String, i As Long, findLower As Boolean strResult = "" findLower = False For i = 1 To Len(strTemp) If (Mid(strTemp, i, 1) Like "[az]") Then findLower = True Else If findLower = True Then strResult = strResult & Mid(strTemp, i) DeletFirstLowerPart = strResult Exit Function End If strResult = strResult & Mid(strTemp, i, 1) End If Next i DeletFirstLowerPart = strResult End Function 

 DeletFirstLowerPart("ABCdefGHI") = "ABCGHI" DeletFirstLowerPart("ABCdefGHIjkl") = "ABCGHIjkl" 

 Private Function DeleteLowerCasesLike(InputString As String) As String Dim i As Integer For i = 1 To Len(InputString) If Mid(InputString, i, 1) Like "[az]" Then InputString = Left(InputString, i - 1) & Mid(InputString, i + 1) i = i - 1 End If Next DeleteLowerCasesLike = InputString End Function 

另一个RegExp解决scheme,需要添加到Microsoft VBScript Regular Expressions 1.0 (在VBA窗口中, Tools->Referances菜单)

 Private Function DeleteLowerCasesRegExp(InputString As String) Dim RE As New RegExp With RE .Global = True .IgnoreCase = False .Pattern = "[az]" DeleteLowerCasesRegExp = .Replace(InputString, "") End With End Function 

另一个解决scheme也不Like使用RegExp

 Private Function DeleteLowerCasesAsc(InputString As String) As String Dim i As Integer For i = 1 To Len(InputString) If Mid(InputString, i, 1) = Empty Then Exit For If Asc(Mid(InputString, i, 1)) >= 97 And Asc(Mid(InputString, i, 1)) <= 122 Then InputString = Left(InputString, i - 1) & Mid(InputString, i + 1) i = i - 1 End If Next DeleteLowerCasesAsc = InputString End Function 

使用replacefunction的另一个解决scheme是:

 Private Function DeleteLowerCasesReplace(InputString As String) As String Dim i As Integer For i = 97 To 122 InputString = Replace(InputString, Chr(i), "") Next DeleteLowerCasesReplace = InputString End Function