我想从列中找到大写字符串,如果可用,则选择前三个字母并以小写字母

时间:2018-04-18 15:19:08

标签: excel excel-formula spreadsheet xls

我想从列中找到大写字符串,如果可用,则选择前三个字母并以小写字母打印。 考虑我在列中的数据如:

 oracle-DATA-key  --> convert it --> oracle-dat-key
key-JAVABEAN    ---> convert it --> key-jav 

我能够找到=NOT(EXACT(LOWER(F5),F5)),如果有资本价值,这将是真的。但这我不想要

2 个答案:

答案 0 :(得分:0)

如下所示,它完全基于单元格中的第一个字符。如果您正在寻找任何大写字母,请不要这样做。

=IFERROR(IF(AND(CODE(F5)<=90,CODE(F5)>=65),LOWER(LEFT(F5,3)),""),"")

一个正则表达式函数,用于输出更改的字符串,更改所有出现次数

Public Function ReplacedString(ByVal r As Range) As String

    With New RegExp
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = "[A-Z][a-z]{2,}"
        outputString = r.Text

        If .test(outputString) Then
            For Each currMatch In .Execute(r.Text)
                outputString = Replace(outputString, currMatch.Value, LCase$(Left(currMatch, 3)))
            Next currMatch
        Else
            ReplacedString = r.Text
        End If
    End With

    ReplacedString = outputString

End Function

使用函数循环目标列:

Public Sub ReplaceStrings()
    Const columnToReplaceIn = 1                  'e.g. A
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1")   'change as appropriate

    Application.ScreenUpdating = False
    With ws
        For Each rng In Intersect(.Columns(columnToReplaceIn), .UsedRange)
            rng.Value = ReplacedString(rng)
        Next rng
    End With
    Application.ScreenUpdating = True
End Sub
Public Function ReplacedString(ByVal r As Range) As String

    With New RegExp 'CreateObject("VBScript.RegExp")   ''Late binding if not Microsoft vbscript regular expressions referenc
        .Global = True
        .MultiLine = True
        .IgnoreCase = False
        .Pattern = "[A-Z][a-z]{2,}"
        outputString = r.Text

        If .test(outputString) Then
            For Each currMatch In .Execute(r.Text)
                outputString = Replace(outputString, currMatch.Value, LCase$(Left(currMatch, 3)))
            Next currMatch
        Else
            ReplaceString = r.Text
        End If
    End With

    ReplacedString = outputString

End Function

答案 1 :(得分:0)

您已经提供了非常小的数据集来进行测试。这是一个使用RegExp

的初步概念
Public Function ReplaceFirstCaps(strInput As String) As String
Dim oM As Object
With CreateObject("VBScript.RegExp")
    .Pattern = "[A-Z]{3,}"
    If .Test(strInput) Then
        Set oM = .Execute(strInput)
        ReplaceFirstCaps = Replace(strInput, oM(0), LCase(Left(oM(0), 3)), , , vbBinaryCompare)
    Else
        ReplaceFirstCaps = strInput
    End If
End With
End Function

然后在表格中使用它:

=ReplaceFirstCaps(A2)