Consulting

Results 1 to 3 of 3

Thread: Help on code found here (InputBox Type 8)

  1. #1
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location

    Help on code found here (InputBox Type 8)

    Hi,

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

    This is the original code
    [VBA]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[/VBA]

    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.

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

    Any help is appreciated.

    T&R

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Not sure if this is what your looking for but this is from a post by Jake:
    [VBA]
    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
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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
    [VBA]
    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
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •