【问题标题】:UserForm to change text to uppercase, lowercase or using proper function用户窗体将文本更改为大写、小写或使用适当的功能
【发布时间】:2016-05-07 15:38:59
【问题描述】:

伙计们!

此时我创建了一个用户窗体来使用三个选项更改文本上的字母:

  1. 大写
  2. 小写
  3. 正确的函数

我的第一个代码使用了 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 将在哪里分配OptionUpperOptionLowerOptionProper?你认为这个值只是因为你添加了Dim OptionSelect As Variant这一行而神奇地出现了吗?
  • 看来这是一个愚蠢的错误。我是 VBA 的新手,但我仍然感谢您的评论。谢谢

标签: vba excel


【解决方案1】:

而不是

Select Case OptionSelect

使用

Select Case True

这将为您提供与 OptionButtons 值匹配的正确值

【讨论】:

  • 非常感谢!但是仍然有一个问题:为什么在这个微软示例中没有使用 Case Select True ?我按照那个例子,认为我的变量 Option Select 会收到 Case Select 的选项。我知道我可能犯了某种错误,但我正在努力识别它。
  • 我应该删除这行 Dim OptionSelect As Variant 因为它是不必要的吗?
【解决方案2】:

第一个代码从某处获取OptionSelect 的值。

如果它在那里工作,那么问题是在你的第二个代码中你有这个额外的行:

Dim OptionSelect As Variant

在这一行中,您将这个值设为空,这样会使Select Case 无用,因为没有值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多