如何使每个字母的字母变成帽子,但不是字母“”“,”和“,”它“,”for“ [英] How to make every letter of word into caps but not for letter "of", "and", "it", "for"?

查看:169
本文介绍了如何使每个字母的字母变成帽子,但不是字母“”“,”和“,”它“,”for“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如医药总监,我想把它当作医药总监,医药总监,我不想要大写的大写字母,请帮助

解决方案

以下VBA代码将是一个好的开始。

 选项Base 1 
选项显式

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

'还原为小写字母的单词列表'

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

'获取正确的字符串,两端都有空格'

result = & WorksheetFunction.Proper(inputString)&

'处理每个单词以恢复为小写'

对于idx = 1 To UBound(lowerWords)
'用水疗恢复每一个字ces在任一侧'

currWord =& lowerWords(idx)&
wordPos = InStr(result,currWord)
虽然wordPos> 0
result = Left(result,wordPos - 1)& LCase(currWord)& Mid(result,wordPos + Len(currWord))
wordPos = InStr(result,currWord)
Wend
下一个

'去掉结尾处的空格'

ProperIsh = Mid(result,2,Len(result) - 2)
结束函数

有一些测试代码:

  Sub test()
MsgBox ProperIsh(HELLO I AM THE LAW,我是一切的小写法))
End Sub

它的作用是适当的每个单词(大写第一个字母,一切小写),然后系统地将任何特殊的单词恢复到所有小写。



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



测试代码生成一个消息框,预期的输出:

 你好我是法律,我是一切的小写法律

为了你在表达式中,将其视为任何其他用户定义的函数,例如:

  = ProperIsh(A1)

您可以使用以下屏幕截图看到它,其中列B使用上面显示的公式: p>

  AB 
1药物主任医师总监
2我是法律我是法律
3让滑溜狗的战争让我们溜冰之战


For example "director of medicine" and I want it as "Director of Medicine not "Director Of Medicine" . I do not want letter "of" to be capitalise. Please help

解决方案

The following VBA code would be a good start.

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

And some test code for it:

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

What it does is to proper-case every word (upper-case first letter, everything else lower-case) then systematically revert any of the special words back to all lower-case.

It presupposes that space is the only separator but could be made more adaptable if that's the case.

The test code generates a message box with the expected output:

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

In order to use it in your expression, treat it as any other user defined function, such as with:

=ProperIsh(A1)

You can see it in operation with the following "screenshot" where column B uses the formula shown above:

             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 

这篇关于如何使每个字母的字母变成帽子,但不是字母“”“,”和“,”它“,”for“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆