Vba 如何将单词中的每个字母都做成大写字母,而不是字母;“的&引用;及&引用;它是&引用;为了“什么?”;?

Vba 如何将单词中的每个字母都做成大写字母,而不是字母;“的&引用;及&引用;它是&引用;为了“什么?”;?,vba,excel,Vba,Excel,例如“医学主任”,我希望它是“医学主任”而不是“医学主任”。我不希望字母“of”大写。请帮助以下VBA代码将是一个良好的开端 Option Base 1 Option Explicit Function ProperIsh(inputString As String) As String Dim result As String Dim currWord As String Dim idx As Integer Dim wordPos As Integer

例如“医学主任”,我希望它是“医学主任”而不是“医学主任”。我不希望字母“of”大写。请帮助以下VBA代码将是一个良好的开端

Option Base 1
Option Explicit

Function ProperIsh(inputString As String) As String
    Dim result As String
    Dim currWord As String
    Dim idx As Integer
    Dim wordPos As Integer

    ' List of words to revert to lower-case '

    Dim lowerWords As Variant
    lowerWords = Array("Of", "And", "It", "For", "Am", "The")

    ' Get proper-cased string with spaces on either end '

    result = " " & WorksheetFunction.Proper(inputString) & " "

    ' Process each word to revert to lower-case '

    For idx = 1 To UBound(lowerWords)
        ' Revert every one of that word with spaces on either side '

        currWord = " " & lowerWords(idx) & " "
        wordPos = InStr(result, currWord)
        While wordPos > 0
           result = Left(result, wordPos - 1) & LCase(currWord) & Mid(result, wordPos + Len(currWord))
           wordPos = InStr(result, currWord)
        Wend
    Next

    ' Get rid of the spaces at the end '

    ProperIsh = Mid(result, 2, Len(result) - 2)
End Function
以及它的一些测试代码:

Sub test()
    MsgBox (ProperIsh("HELLO I AM THE LAW and i am the lower case law of everything"))
End Sub
它所做的是将每个单词的大小写正确(大写字母,其他字母小写),然后系统地将任何特殊单词还原为所有小写字母

它假定空间是唯一的分隔物,但如果是这样的话,可以使空间更具适应性

测试代码生成具有预期输出的消息框:

Hello I am the Law and I am the Lower Case Law of Everything
要在表达式中使用它,请将其视为任何其他用户定义的函数,例如:

=ProperIsh(A1)
您可以通过以下“屏幕截图”看到它的运行,其中B列使用了上述公式:

             A                          B
1   director of medicine       Director of Medicine 
2   I am the law               I am the Law 
3   Let slip the dogs of war   Let Slip the Dogs of War 
我将其用作创建大写例外列表的参考。

Function TitleCase
使用
WorksheetFunction.ProperCase
对文本进行预处理。因此,我对缩略设置了一个例外,因为
WorksheetFunction.ProperCase
不正确地将其大写。

每个句子中的第一个单词和双引号后的第一个单词将保持大写。标点符号也会正确处理。

Function TitleCase(text As String) As String
    Dim doc
    Dim sentence, word, w
    Dim i As Long, j As Integer
    Dim arrLowerCaseWords

    arrLowerCaseWords = Array("a", "an", "and", "as", "at", "but", "by", "for", "in", "of", "on", "or", "the", "to", "up", "nor", "it", "am", "is")

    text = WorksheetFunction.Proper(text)

    Set doc = CreateObject("Word.Document")
    doc.Range.text = text

    For Each sentence In doc.Sentences
        For i = 2 To sentence.Words.Count
            If sentence.Words.Item(i - 1) <> """" Then
                Set w = sentence.Words.Item(i)
                For Each word In arrLowerCaseWords
                    If LCase(Trim(w)) = word Then
                        w.text = LCase(w.text)
                    End If

                    j = InStr(w.text, "'")

                    If j Then w.text = Left(w.text, j) & LCase(Right(w.text, Len(w.text) - j))

                Next
            End If
        Next
    Next

    TitleCase = doc.Range.text

    doc.Close False
    Set doc = Nothing
End Function
函数标题库(文本为字符串)为字符串
暗号
模糊的句子、单词、w
Dim i等于长,j等于整数
低字母
arrLowerCaseWords=数组(“a”、“an”、“and”、“as”、“at”、“but”、“by”、“for”、“in”、“of”、“on”、“or”、“the”、“to”、“up”、“nor”、“it”、“am”、“is”)
text=工作表函数。正确(text)
Set doc=CreateObject(“Word.Document”)
doc.Range.text=文本
文档句子中的每个句子
对于i=2到句子.Words.Count
如果句子.Words.Item(i-1)为“”,则
集合w=句子、单词、项目(i)
用小写字母表示每个单词
如果LCase(Trim(w))=单词,则
w、 text=LCase(w.text)
如果结束
j=仪表(w.text,“”)
如果j那么w.text=左(w.text,j)和LCase(右(w.text,Len(w.text)-j))
下一个
如果结束
下一个
下一个
TitleCase=doc.Range.text
文件关闭错误
设置文档=无
端函数

我们不是通灵者,正如我们所希望的那样。你没有给出上下文或代码,也没有问题或细节。不清楚。我们帮不了你。当使用-ish作为后缀时,没有必要大写;D