我有一些代码可以将我的文本转换为Proper Case,但它不起作用。 我首先标记要转换的单元格,然后运行此宏:
HELLO
但Excel给了我runtimer错误91
对象变量或未设置块变量
答案 0 :(得分:3)
您的代码将覆盖公式 - 告诉它忽略公式
而不是使用WorksheetFunction.Proper
使用StrConv(value, conversion_type)
。
Sub ProperCase()
Dim rng As Range
'Use special cells so as not to overwrite formula.
For Each rng In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells
Select Case StrConv(rng.Value, vbLowerCase)
'Ensure all exception words are lower case.
Case "bob", "word2", "dave", "some other words"
rng.Value = StrConv(rng.Value, vbLowerCase)
Case Else
'StrConv is the VBA version of Proper.
rng.Value = StrConv(rng.Value, vbProperCase)
End Select
Next rng
End Sub
修改:添加SELECT...CASE
以根据评论排除字词。
答案 1 :(得分:0)
cell.value
:
Sub ProperCase()
Dim rng As Range, cell As Range
Set rng = Selection
For Each cell In rng
cell.Value = WorksheetFunction.Proper(cell.Value)
Next cell
End Sub