使用VBA转换为句子案例 [英] Converting to sentence case using VBA
问题描述
我一直在谷歌浏览网页,在这里寻找一个解决这个看似简单的请求的解决方案,但无济于事。有人知道使用 vba的问题将字符串转换为句子的可靠方法?
理想情况下,我会将其构建成子而不是功能,所以从GUI调用更容易。
为了参考,我想要:
这里是一个长期的,超大的上限。请立即修改。
成为:
这是一个长而丑的大写句子。请立即修改我。
转换为标题案例我发现非常简单(因为它有一个内置的功能),但转换为我已经尝试了以下一些方法,但是在每一回合都会出现错误:
- http://www.vbforums.com /showthread.php?t=536912
- http://vbamacros.blogspot.com/2007/09/sentence-case.html
您可以使用RegExp来更有效地运行解析
这样的事情
Sub Tested()
调用ProperCaps(here LONG,UGLY UPPERCASE SENTENCE。请立即修正。& vbCrLf&am p;下一行!现在)
End Sub
函数ProperCaps(strIn As String)As String
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
设置objRegex = CreateObject(vbscript.regexp)
strIn = LCase $(strIn)
带objRegex
.Global = True
.ignoreCase = True
.Pattern =(^ | [\\\\\\\\\\??((az))
如果.test(strIn)然后
设置objRegMC = .Execute(strIn)
对于每个objRegM在objRegMC
中间$(strIn,objRegM.firstindex + 1,objRegM.Length)= UCase $(objRegM)
下一个
结束如果
MsgBox strIn
结束
结束函数
I've been trawling through page after page on Google and here looking for a solution to this seemingly simple request, but to no avail. Does anyone know a reliable way to convert a string to sentence case using vba?
Ideally I would build it into a sub rather than a function, so it is easier to call from the GUI.
For reference, I would want:
HERE IS A LONG, UGLY UPPERCASE SENTENCE. PLEASE AMEND ME IMMEDIATELY.
to become:
Here is a long, ugly uppercase sentence. Please amend me immediately.
Converting to Title Case I found extremely simple (as there's a built-in function for that) but converting to sentence case has proven really difficult indeed.
I have tried some of the following methods but come up with errors at every turn:
- http://www.vbforums.com/showthread.php?t=536912
- http://vbamacros.blogspot.com/2007/09/sentence-case.html
How can I get this to work?
You could use a RegExp to more efficiently run the parsing
Something like this
Sub Tested()
Call ProperCaps("HERE IS A LONG, UGLY UPPERCASE SENTENCE. PLEASE AMEND ME IMMEDIATELY." & vbCrLf & "next line! now")
End Sub
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
End Function
这篇关于使用VBA转换为句子案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!