VBA转换为大小写跳过某些单词
目前我能够将一系列单元格转换为正确的大小写。我需要在转换为正确的大小写时跳过单元格中的某些单词 示例:从此处获取将转换为从此处获取 但“的”一词不应转换为适当的大小写。 能做到吗 下面是我编写的代码,用于将范围转换为适当的大小写VBA转换为大小写跳过某些单词,vba,excel,Vba,Excel,目前我能够将一系列单元格转换为正确的大小写。我需要在转换为正确的大小写时跳过单元格中的某些单词 示例:从此处获取将转换为从此处获取 但“的”一词不应转换为适当的大小写。 能做到吗 下面是我编写的代码,用于将范围转换为适当的大小写 Sub Processproper() Dim Rng As Range Dim WorkRng As Range Dim xTitleId On Error Resume Next xTitleId = "SelectRange" Set WorkRng
Sub Processproper()
Dim Rng As Range
Dim WorkRng As Range
Dim xTitleId
On Error Resume Next
xTitleId = "SelectRange"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Select Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
If IsEmpty(Rng) = True Then
Else
Rng.Value = StrConv(Rng.Value, vbProperCase)
End If
Next
end sub
快速搜索给了我这样一个信息:缩进,或者更确切地说,缺少缩进,使得代码比需要的更难阅读。谢谢Scott Craner我从你建议的链接中获得了函数,但是如何在我的宏中调用它,请你帮忙。非常感谢,,,,这真的让我高兴了一天:@tharun你有没有在我的答案中打勾,把它标记为正确答案?
Sub Processproper()
Dim Rng As Range
Dim WorkRng As Range
Dim xTitleId
On Error Resume Next
xTitleId = "SelectRange"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Select Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
If IsEmpty(Rng) = True Then
Else
Rng.Value = Title(Rng)
End If
Next
End Sub
Function Title(ByVal ref As Range) As String
Dim vaArray As Variant
Dim c As String
Dim i As Integer
Dim J As Integer
Dim vaLCase As Variant
Dim str As String
' Array contains terms that should be lower case
vaLCase = Array("a", "an", "and", "in", "is", _
"of", "or", "the", "to", "with")
c = StrConv(ref, 3)
'split the words into an array
vaArray = Split(c, " ")
For i = (LBound(vaArray) + 1) To UBound(vaArray)
For J = LBound(vaLCase) To UBound(vaLCase)
' compare each word in the cell against the
' list of words to remain lowercase. If the
' Upper versions match then replace the
' cell word with the lowercase version.
If UCase(vaArray(i)) = UCase(vaLCase(J)) Then
vaArray(i) = vaLCase(J)
End If
Next J
Next i
' rebuild the sentence
str = ""
For i = LBound(vaArray) To UBound(vaArray)
str = str & " " & vaArray(i)
Next i
Title = Trim(str)
End Function