简体   繁体   English

在Excel VBA中剥离大写单词

[英]Stripping Uppercase Words in Excel VBA

Stripping Uppercase Words in Excel VBA 在Excel VBA中剥离大写单词

I have an Excel sheet like this one: 我有一张这样的Excel工作表:

A        B
1        Used CONTENT VERSION SYSTEM for the FALCON Project
2        USA beats UK at Soccer Cup 2008
3        DARPA NET’s biggest contribution was the internet
4        One big problem is STRUCTURED QUERY LANGUAGE queries on non-normalized data

I want to extract all of the words in UPPERCASE and generate a list with them: 我想提取大写中的所有单词并使用它们生成一个列表:

A                             B
CONTENT VERSION SYSTEM        1
FALCON                        1
USA                           2
UK                            2
DARPA NET                     3
STRUCTURED QUERY LANGUAGE     4

I was thinking that I could check if “eachWord” == UCase(eachWord), but I don't know how to handle phrases. 我当时以为我可以检查“ eachWord”是否== UCase(eachWord),但是我不知道该如何处理短语。 I also don't know how to handle phrases that end in “apostrophe s”, “end parenthesis”, or punctuation. 我也不知道该如何处理以“撇号”,“结束括号”或标点符号结尾的短语。

I've been splitting words like this: IndividualWordsArray = Split(ActiveSheet.Cells(workingRow, 2).Value) 我一直在拆分这样的单词: IndividualWordsArray = Split(ActiveSheet.Cells(workingRow, 2).Value)

But that only makes an array based on space chars. 但这只能构成一个基于空格的数组。 I thought it might help if, in addition to spaces, it could also split by these chars: “ ( ) : ' , . 我认为,除空格以外,如果还可以将这些字符分开,则可能会有所帮助:“():',。 ? ! ; ; After some searching, I find I can split a line by a char other than spaces, but only one delimiter at a time. 经过一些搜索后,我发现我可以用除空格以外的一个字符来分隔一行,但一次只能一个定界符。

Anyone have any idea how to create a list with all of the uppercase words and phrases? 任何人都不知道如何用所有大写单词和短语创建列表?

一种简单的方法是获取文本副本,将所有定界符替换为空格字符,然后使用空格作为定界符进行分割。

Here's an ugly slow way, but it does work (except it won't return NET from NET's). 这是一个丑陋的慢速方法,但是它确实可以工作(除非它不会从NET返回NET)。 I just loop through the array of words and test each letter for caps. 我只是遍历单词数组并测试每个字母的上限。 The Option Compare Binary statement is crucial. Option Compare Binary语句至关重要。

Option Explicit
Option Compare Binary

Sub x()
    Dim IndividualWordsArray() As String, keeperArray() As String
    Dim i As Integer, j As Integer, k As Integer
    Dim allCaps As Boolean

    IndividualWordsArray = Split(ActiveCell)
    k = 0
    For i = 0 To UBound(IndividualWordsArray)
        allCaps = True
        For j = 1 To Len(IndividualWordsArray(i))
            If Not Mid(IndividualWordsArray(i), j, 1) Like "[A-Z]" Then
                allCaps = False
                Exit For
            End If
        Next j
        If allCaps Then
            ReDim Preserve keeperArray(k)
            keeperArray(k) = IndividualWordsArray(i)
            Debug.Print keeperArray(k)
            k = k + 1
        End If
    Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM