excel-vba – Excel VBA句子案例功能

我在以下链接中找到了关于句子功能的旧帖子.

  
Converting to sentence case using vba

我碰巧喜欢bretdj设计的以下功能

Function ProperCaps(strIn As String) As String
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Set objRegex = CreateObject("vbscript.regexp")
strIn = LCase$(strIn)
With objRegex
    .Global = True
    .ignoreCase = True
     .Pattern = "(^|[\.\?\!\r\t]\s?)([a-z])"
    If .test(strIn) Then
        Set objRegMC = .Execute(strIn)
        For Each objRegM In objRegMC
            Mid$(strIn, objRegM.firstindex + 1, objRegM.Length) = UCase$(objRegM)
        Next
    End If
    MsgBox strIn
End With

我无法弄清楚的是如何使句子的功能成为特定单元格中输入的字符串,然后将更正的句子放回原始单元格.我不需要它在消息框中.类似于以下内容:

If Not Intersect(Target, myrange2) Is Nothing Then
    Target.Value = ProperCaps(Target.Value)
End If

任何帮助,将不胜感激.请原谅我重新发布此事,我无权对帖子发表评论.

谢谢
加里

最佳答案 你的函数缺少最后一部分,但如果最后一部分只是一行End函数,那么你需要做的就是用ProperCaps = strIn替换MsgBox strIn:

Option Explicit

Function ProperCaps(strIn As String) As String

    Dim objRegex As Object
    Dim objRegMC As Object
    Dim objRegM As Object

    Set objRegex = CreateObject("vbscript.regexp")
    strIn = LCase$(strIn)

    With objRegex
        .Global = True
        .ignoreCase = True
        .Pattern = "(^|[\.\?\!\r\t]\s?)([a-z])"

        If .test(strIn) Then
            Set objRegMC = .Execute(strIn)

            For Each objRegM In objRegMC
                Mid$(strIn, objRegM.firstindex + 1, objRegM.Length) = UCase$(objRegM)
            Next
        End If
    End With

    ProperCaps = strIn

End Function
点赞