如何将每个字母的字母变成大写字母而不是字母“of”,“and”,“it”,“for”?

比如“医学院的主任”,我希望它是“医学主任而不是医学主任”,我不希望大写字母“大写”,请帮助

下面的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 

还有一些testing代码:

 Sub test() MsgBox (ProperIsh("HELLO I AM THE LAW and i am the lower case law of everything")) End Sub 

它所做的是正确处理每个单词(大写首字母,其他所有小写),然后系统性地将任何特殊单词还原为全部小写。

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

testing代码会生成一个带有预期输出的消息框:

 Hello I am the Law and I am the Lower Case Law of Everything 

为了在expression式中使用它,请将其作为任何其他用户定义的函数处理,例如:

 =ProperIsh(A1) 

你可以在下面的屏幕截图中看到它在B列使用上面显示的公式:

  AB 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 

我在“文章标题”中使用“大写规则”作为创build大写exception列表的参考。

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