如何处理 VBA 用户窗体中 RefEdit 控件的 blank/Invalid 数据或如何检查 Range() 是 valid/not VBA 中的错误

How to handle blank/Invalid data for RefEdit control in VBA userform Or How to check Range() is valid/not Error in VBA

我正在使用 Excel VBA 用户表单来更改字体大小写,如下图所示。当 RefEdit 控件具有正确的范围时,它工作正常。但是,如果我单击“应用”按钮,使 RefEdit 保持空白/仅 Space/任何单词(无效范围),用户窗体就会消失,而不会显示任何错误通知。

对于大写代码:-

Sub UpperCaseFont()
        For Each x In Range(CaseRefEdit.Value)
        If Not IsEmpty(x.Value) Then
            x.Value = UCase(x.Value)
        End If
        Next
MsgBox "Done"
End Sub

小写代码:-

Sub LowerCaseFont()
    For Each x In Range(CaseRefEdit.Value)
    If Not IsEmpty(x.Value) Then
        x.Value = LCase(x.Value)
    End If
    Next
MsgBox "Done"
End Sub

大写代码:-

Sub ProperCaseFont()
    For Each x In Range(CaseRefEdit.Value)
    If Not IsEmpty(x.Value) Then
        x.Value = WorksheetFunction.Proper(x.Value)
    End If
    Next
End Sub

命令按钮代码:-

Private Sub CaseApplyCommandButton_Click()
    If UpperCase = True Then Call UpperCaseFont
    If LowerCase = True Then Call LowerCaseFont
    If ProperCase = True Then Call ProperCaseFont

出于这个原因,我尝试进行如下修改,但我仍然面临如果 RefEdit 为空并且我单击“应用”按钮然后用户窗体消失并且还发现其他所有用户窗体启动未知问题的问题初始化。

Private Sub CaseApplyCommandButton_Click()
'Font Case
Dim Rng As Range
On Error Resume Next
Set Rng = Range(Me.CaseRefEdit.Value)
MsgBox Rng
On Error GoTo 0
If Rng Is Nothing Then
    MsgBox "Select the Cells to change the case"
Else
    If UpperCase = True Then Call UpperCaseFont
    If LowerCase = True Then Call LowerCaseFont
    If ProperCase = True Then Call ProperCaseFont
End If

End Sub

我发现当我添加以下代码时问题开始了:-

Private Sub CaseRefEdit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Range(CaseRefEdit.Value).Select
End Sub

根据我的理解,当 RefEdit 没有给出任何数字或单词的输入范围或保持空白时,用户表单就会消失。有什么解决办法吗?

您可以使用 On Error 语句,例如:

Function testRNG(addr As String) As Range   ' returns correct Range object or Nothing otherwise
    ' initially testRNG = Nothing
    On Error Resume Next
    Set testRNG = Range(addr)   ' if the address is correct, testRNG returns Range, otherwise an error occurs, the expression is not evaluated and testRNG remains Nothing
End Function

Sub foo() 
    Dim addr As String
    addr = "W12"
    Set Rng = testRNG(addr)
    If Rng Is Nothing Then
        Debug.Print "Address " & addr & " is wrong"
    Else
        Debug.Print "Range " & Rng.Address & " is correct"
    End If
End Sub

我的问题解决如下。谢谢 @Алексей Р.

Private Sub CaseRefEdit_Exit(ByVal Cancel As MSForms.ReturnBoolean)
On error resume next    
Range(CaseRefEdit.Value).Select
End Sub

有人知道,如何检查 Range("etc")/ Range(123) 是否有效?赞-

If IsError(range("etc")) then
...
else
....
end if

关于第二个问题。 [if IsError (range ("Hello") then ...] 表达式首先计算 range ("Hello"),如果它无效,调用函数前会出错,所以最好将范围的地址传递给IsError函数,在函数内部计算范围并判断其正确性。

Function IsError(addr As String) As Boolean
    Dim rng As Range
    On Error Resume Next
    Set rng = Range(addr)
    IsError = rng Is Nothing
End Function

Sub test1()
    If IsError("%$%W34") Then
        Debug.Print "Range is invalid"
    Else
        Debug.Print "Range is correct"
    End If
End Sub