例如"医学主任"我希望它作为"医学主任不是"医学主任" 。我不想要"""要资本化。请帮忙
答案 0 :(得分:1)
以下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
答案 1 :(得分:0)
我使用Rules for Capitalization in Titles of Articles作为参考来创建大写异常列表。
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