PDA

View Full Version : Help on code found here (InputBox Type 8)



lynnnow
06-10-2006, 09:33 AM
Hi,

I had seen a code here where the user can use the InputBox function to get a range.

This is the original code
Sub GetUserRange()
Dim UserRange As Range

Prompt = "Select a cell for the output."
Title = "Select a cell"

' Display the Input Box
On Error Resume Next
Set UserRange = Application.InputBox( _
Prompt:=Prompt, _
Title:=Title, _
Default:=ActiveCell.Address, _
Type:=8) 'Range selection

' Was the Input Box canceled?
If UserRange Is Nothing Then
MsgBox "Action Canceled"
Else
:
:
End If

End Sub

My problem is that i've not made any change to the set statement of the InputBox, however, the last two weeks has seen a change in the behavior of the code. The set statement always returns "Nothing" instead of the range I've selected. I've stepped through the code many times to check what is going wrong but fail to see any bug.:bug:

I've finally upgraded to Office 2003 on Win 2K.

Any help is appreciated.

T&R: pray2:

lucas
06-10-2006, 09:47 AM
Not sure if this is what your looking for but this is from a post by Jake:

Sub Test()

Dim Selectedarea As Range
Dim MyErr As Long

On Error Resume Next
Set Selectedarea = Application.InputBox(prompt:="Select the cell/range...", _
Title:="SWITCH CELL SELECTION", Default:=Selection.Address, Type:=8)
MyErr = Err
On Error GoTo 0
If MyErr <> 0 Then
MsgBox "You press Cancel, this procedure will now terminate."
Exit Sub
End If

MsgBox Selectedarea.Cells.Count 'Change to do what you want if Cells.Count = 1

End Sub

lucas
06-10-2006, 09:53 AM
After looking at this is seem that your code works just fine. It just needs you to tell it to do something with the range....I added one line to your code..in red letters

Sub GetUserRange()
Dim UserRange As Range

Prompt = "Select a cell for the output."
Title = "Select a cell"

' Display the Input Box
On Error Resume Next
Set UserRange = Application.InputBox( _
Prompt:=Prompt, _
Title:=Title, _
Default:=ActiveCell.Address, _
Type:=8) 'Range selection

' Was the Input Box canceled?
If UserRange Is Nothing Then
MsgBox "Action Canceled"
Else
:
:
End If
MsgBox UserRange.Cells.Count 'Change to do what you want if Cells.Count = 1
End Sub