【发布时间】:2016-05-07 15:38:59
【问题描述】:
伙计们!
此时我创建了一个用户窗体来使用三个选项更改文本上的字母:
- 大写
- 小写
- 正确的函数
我的第一个代码使用了 If-Then 结构,没问题。我把它放在下面:
Private Sub OkButton_Click()
Dim WorkRange As Range
Dim cell As Range
'Detects only constant type (text; excludes formulas)
On Error Resume Next
Set WorkRange = Selection.SpecialCells(xlCellTypeConstants, xlCellTypeConstants)
'Uppercase
If OptionUpper Then
For Each cell In WorkRange
cell.Value = UCase(cell.Value)
Next cell
End If
'Uppercase
If OptionLower Then
For Each cell In WorkRange
cell.Value = LCase(cell.Value)
Next cell
End If
'Using Proper Function
If OptionProper Then
For Each cell In WorkRange
cell.Value = Application.WorksheetFunction.Proper(cell.Value)
Next cell
End If
Unload UserForm1
End Sub
Private Sub UserForm_Click()
End Sub
我使用了一种模式来运行 UserForm1:
Sub ChangeCase2()
If TypeName(Selection) = "Range" Then
UserForm1.Show
Else
MsgBox "Selection a range.", vbCritical
End If
End Sub
一切正常。但后来我想:是否可以使用 Select Case 结构?所以我尝试了,不幸的是,没有跑。大写选项作为小写,而小写和正确作为大写。我查看了我给按钮的标题,一切都很好。有人能帮帮我吗?
Private Sub CancelButton_Click()
Unload UserForm2
End Sub
Private Sub OkButton_Click()
Dim WorkRange As Range
Dim cell As Range
Dim OptionSelect As Variant
On Error Resume Next
Set WorkRange = Selection.SpecialCells(xlCellTypeConstants, xlCellTypeConstants)
Select Case OptionSelect
Case OptionUpper 'Letras Maiúsculas
For Each cell In WorkRange
cell.Value = UCase(cell.Value)
Next cell
Case OptionLower 'Letras Minúsculas
For Each cell In WorkRange
cell.Value = LCase(cell.Value)
Next cell
Case OptionProper 'Iniciais Maiúsculas
For Each cell In WorkRange
cell.Value = Application.WorksheetFunction.Proper(cell.Value)
Next cell
End Select
Unload UserForm2
End Sub
我对 UserForm2 使用了另一种模式:
Sub ChangeCase3()
If TypeName(Selection) = "Range" Then
UserForm2.Show
Else
MsgBox "Selection a range.", vbCritical
End If
End Sub
【问题讨论】:
-
您认为
OptionSelect将在哪里分配OptionUpper、OptionLower或OptionProper?你认为这个值只是因为你添加了Dim OptionSelect As Variant这一行而神奇地出现了吗? -
看来这是一个愚蠢的错误。我是 VBA 的新手,但我仍然感谢您的评论。谢谢