在Excel上提取大写单词

问题描述 投票:0回答:2

我有167个单元格,每个单元格都有一串文本,每个单元格都有一个全部大写的单词,我只需要将该单词复制到一个新单元格。我已经尝试了EXACT公式,但它只识别文本是否有大写单词并返回“true”或“false”。

例如:

A1:快速的棕色狐狸跳过懒狗

结果应该是:

B1:JUMPS

excel formula uppercase
2个回答
2
投票

请尝试以下用户定义的功能:

Public Function grabber(s As String) As String
    grabber = ""
    arry = Split(s, " ")
    For Each a In arry
        If a = UCase(a) Then
            grabber = a
            Exit Function
        End If
    Next a
End Function

它将提取单元格中的第一个大写单词。

enter image description here

用户定义函数(UDF)非常易于安装和使用:

  1. ALT-F11调出VBE窗口
  2. ALT-I ALT-M打开一个新模块
  3. 粘贴内容并关闭VBE窗口

如果保存工作簿,UDF将随之保存。如果您在2003年之后使用的是Excel版本,则必须将文件另存为.xlsm而不是.xlsx

要删除UDF:

  1. 如上所述调出VBE窗口
  2. 清除代码
  3. 关闭VBE窗口

要从Excel使用UDF:

= myfunction的(A1)

要了解有关宏的更多信息,请参阅:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

有关UDF的详细信息,请参阅:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

必须启用宏才能使其正常工作!

(可以很容易地修改此代码以从句子中提取所有大写单词)


0
投票

您可以使用正则表达式来提取大写单词。这可以在工作表中部署为UDF

Option Explicit
Public Sub TEST()
    Dim tests(), i As Long
    tests = Array("The lazy LAD was sorry for the debacle", "She wept as her FLAXEN hair tumbled down the parapet")

    For i = LBound(tests) To UBound(tests)
        Debug.Print GetString(tests(i))
    Next
End Sub

Public Function GetString(ByVal inputString As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .MultiLine = True
        .Pattern = "\b[A-Z]+\b"
        If .TEST(inputString) Then
            If len(.Execute(inputString)(0)) > 1 Then
                GetString = .Execute(inputString)(0)
                Exit Function
            End If     
        End If
        GetString = vbNullString
    End With
End Function

正则表达式:

试试吧here

\b在单词边界(^\w|\w$|\W\w|\w\W)断言位置

匹配[A-Z]+下面列表中的单个字符

+ Quantifier - 尽可能多次匹配一次和无限次,根据需要回馈(贪婪)

A-Z A(索引65)和Z(索引90)之间的单个字符(区分大小写)

\b在单词边界(^\w|\w$|\W\w|\w\W)断言位置


在表格中:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.