2013-10-26 37 views
6

我有以下的代碼:柄取消選擇範圍

dim selectRange as Range 
Set selectRange = Application.InputBox("Select your range", "Hello", , , , , , 8) 

當用戶選擇取消的InputBox提示,它返回error of Object not set

我試圖使用Variant變量類型,但我無法處理它。如果取消,則返回False,同時在選擇範圍的情況下,返回InputBox的範圍。

我該如何避免這個錯誤?

+0

問題不在這兩行代碼中,它可能是遵循它們的行。向我們展示這些線條。 – RBarryYoung

回答

4

這是使用輸入框選擇範圍時出現的問題。返回範圍之前,Excel會返回一個錯誤,並且在您按下取消按鈕時會出現此錯誤。

因此,您應該積極處理此錯誤。如果你不希望發生什麼事,當你點擊取消,你可以使用這樣的代碼:

Sub SetRange() 
    Dim selectRange As Range 

    On Error Resume Next 
     Set selectRange = Application.InputBox("Select your range", "Hello", , , , , , 8) 
    Err.Clear 
    On Error GoTo 0 
End Sub 
+0

感謝你這樣做,我認爲它不得不像這樣處理vbcancel上的錯誤,但沒有其他選擇。 – Raystafarian

1

我發現,檢查了「所需的對象」,你提到的錯誤是處理的一種方式取消。

On Error Resume Next 
dim selectRange as Range 
' InputBox will prevent invalid ranges from being submitted when set to Type:=8. 
Set selectRange = Application.InputBox("Select your range", "Hello", , , , , , 8) 
' Check for cancel: "Object required". 
If Err.Number = 424 Then 
    ' Cancel. 
    Exit Sub 
End If 
On Error GoTo 0 
4

我來晚了這裏的聚會,但是這是我唯一能找到的地方,解釋爲什麼我有隻檢查我的任何變量的麻煩。正如在接受的答案中所解釋的,範圍對象上的vbCancel的處理方式與字符串對象的處理方式不同。錯誤必須被錯誤處理程序捕獲。

I 討厭錯誤處理程序。所以我把它隔離到其自身的功能

Private Function GetUserInputRange() As Range 
    'This is segregated because of how excel handles cancelling a range input 
    Dim userAnswer As Range 
    On Error GoTo inputerror 
    Set userAnswer = Application.InputBox("Please select a single column to parse", "Column Parser", Type:=8) 
    Set GetUserInputRange = userAnswer 
    Exit Function 
inputerror: 
    Set GetUserInputRange = Nothing 
End Function 

現在在我的主子,我可以

dim someRange as range 
set someRange = GetUserInputRange 
if someRange is Nothing Then Exit Sub 

總之,這是不一樣的接受的答案,因爲它允許用戶僅處理此錯誤與一個特定的錯誤處理程序,不需要resume next或者以相同的方式處理其餘的程序。如果有人像我一樣在這裏結束。

4

雖然這個問題有點老,我仍然想要顯示適當的方式來做到沒有錯誤。你可以通過功能或子功能來做到這一點。

你的主要過程是這樣的:

Sub test() 
    Dim MyRange As Range 

    testSub Application.InputBox("dada", , , , , , , 8), MyRange 'doing via Sub 
    Set MyRange = testFunc(Application.InputBox("dada", , , , , , , 8)) ' doing via function 

    If MyRange Is Nothing Then 
    Debug.Print "The InputBox has been canceled." 
    Else 
    Debug.Print "The range " & MyRange.Address & " was selected." 
    End If 
End Sub 

三通(搞笑)將是:

Sub testSub(ByVal a As Variant, ByRef b As Range) 
    If TypeOf a Is Range Then Set b = a 
End Sub 

而且功能會是什麼樣子:

Function testFunc(ByVal a As Variant) As Range 
    If TypeOf a Is Range Then Set testFunc = a 
End Function 

現在簡單地按你喜歡的方式選擇並刪除未使用的行。

如果調用子函數或函數,則不需要參數Set。也就是說,InputBox是否返回對象並不重要。你所需要做的就是檢查參數是否是你想要的對象,然後相應地處理它。

編輯

另一種聰明的方法是使用像這樣的集合相同的行爲:

Sub test() 
    Dim MyRange As Range 
    Dim MyCol As New Collection 

    MyCol.Add Application.InputBox("dada", , , , , , , 8) 
    If TypeOf MyCol(1) Is Range Then Set MyRange = MyCol(1) 
    Set MyCol = New Collection 

    If MyRange Is Nothing Then 
    Debug.Print "The inputbox has been canceled" 
    Else 
    Debug.Print "the range " & MyRange.Address & " was selected" 
    End If 
End Sub 

如果您還有任何疑問,就問;)