Consulting

Results 1 to 12 of 12

Thread: Help:User interaction when code running

  1. #1

    Help:User interaction when code running

    I'm getting stuck with this issues and need you all help:

    I'm intending to write a sub, in which the code will ask user to chose some sheet, or range or ... anything in the work sheet. And it wait your user input before running.

    InputBox does not support Range, sheets. And it even not allow us to select anything.

    Is there anyway to do that??

    Please help!!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Few problems there, how will you know when it has been input?

    You can use Application.InputBox or a modal userform.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Have found the following code in google...
    Just a small question. Is there anyway to select multicells in stead of range? this code just get range even if I choose multi cells, it will return a range with the same cells number that I chosen

    I means, how can I choose multi cells, then get their value, and put the value to a new range have the same cells with the cells I chosen.

    Thanks..

    [vba]Function InputRange(Optional Prompt As String = "", Optional Title As String = "", _
    Optional Force As Boolean = False) As Range
    Dim retry As Boolean
    On Error Resume Next
    Do
    Set InputRange = Application.InputBox(Prompt:=Prompt, Title:=Title, Type:=8)
    If InputRange Is Nothing And Force And Not retry Then
    retry = True
    Prompt = "YOU MUST SELECT A RANGE!" & Chr(13) & Prompt
    End If
    Loop While InputRange Is Nothing And Force
    On Error GoTo 0
    End Function[/vba]

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,776
    This shows some of the ways to deal with the range value returned by the Application.InputBox.
    Sub test()
    Dim xRange As Range
    Dim inRange As Range
    
    Set inRange = inputRange("test input"):Rem calls your function
    
    MsgBox "You selected a range " & _
        inRange.Rows.Count & " rows by " & _
        inRange.Columns.Count & " columns."
        
    For Each xRange In inRange
        MsgBox xRange.Value
    Next xRange
    
    With Range("a1")
        Range(.Cells(1, 1), .Cells(inRange.Rows.Count, inRange.Columns.Count)) = inRange.Value
    End With
    
    End Sub

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    How about using RefEdit on a userform.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by yurble_vn
    Have found the following code in google...
    Just a small question. Is there anyway to select multicells in stead of range? this code just get range even if I choose multi cells, it will return a range with the same cells number that I chosen

    I means, how can I choose multi cells, then get their value, and put the value to a new range have the same cells with the cells I chosen.

    Thanks.. [/vba]
    By multi-cells, do you mean to refer to non-contiguous ranges such as
    A1, A7, B3:D7, and G1:H10
    i.e. [VBA]Range("A1,A7,B37,G1:H10")[/VBA]here?
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    By multi-cells, do you mean to refer to non-contiguous ranges such as
    Yep, but how to get it as input for another function? Because the inputbox always return contiguous range

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Use a RefEdit control on a userform as MD suggested.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,321
    Location
    I agree....what could be easier than that?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Example how-to:

    [vba]
    Sub a()
    Dim SelectedRanges As Range
    Set SelectedRanges = Application.InputBox("Select your ranges - use " & _
    "commas to separate non-contiguous ranges", , "A1,B2:C9,F3,z11", , , , , 8)
    SelectedRanges.Select
    MsgBox SelectedRanges.Address
    End Sub
    [/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  11. #11
    Thanks, you all are so kind...

    How about select sheets?

  12. #12
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,776
    Dim mySheet as Worksheet
    Set mySheet = Application.InputBox("pick a cell on the sheet", type:=8).Parent

Posting Permissions

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