雖然這個問題有點老,我仍然想要顯示適當的方式來做到沒有錯誤。你可以通過功能或子功能來做到這一點。
你的主要過程是這樣的:
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
如果您還有任何疑問,就問;)
問題不在這兩行代碼中,它可能是遵循它們的行。向我們展示這些線條。 – RBarryYoung