Consulting

Results 1 to 16 of 16

Thread: Solved: Select Range with variables

  1. #1

    Solved: Select Range with variables

    Hi Guys

    Basically i just want a range which is not set, but can be defined by 2 inputboxes (one for the (X:y) coordinate and one for the (x:Y) coordinate and then automatically get selected.

    heres my code so far. i just can not define the range through those inpboxes....

    [VBA]Dim x As String
    Dim y as String

    x = InputBox("please enter first range coordinate")
    y = InputBox("please enter 2nd range coordinate")

    copyrange = (x:y) 'this is, where it doesnt work)


    With Worksheets("sheet1")
    copyrange.Select
    End With[/VBA]

    it must be possible somehow....thanks for having a look, guys!

    cheers

    ben

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You need to get the numerical elements of the coordinates
    [VBA]
    Sub GetRange()
    Dim x As String
    Dim y As String
    Dim Rng1 As Range
    Dim Rng2 As Range

    x = InputBox("please enter first range coordinate")
    y = InputBox("please enter 2nd range coordinate")

    Set Rng1 = Cells(CLng(Split(x, ",")(0)), CLng(Split(x, ",")(1)))
    Set Rng2 = Cells(CLng(Split(y, ",")(0)), CLng(Split(y, ",")(1)))
    'for debug
    Range(Rng1, Rng2).Select
    Range(Rng1, Rng2).Copy
    End Sub

    [/VBA]
    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'

  3. #3
    Why don't you just use :-

    [VBA] Dim sRange As String
    sRange = Application.InputBox("Get Range ", "Title", , , , , , 8).Address
    [/VBA]
    2+2=9 ... (My Arithmetic Is Mental)

  4. #4
    Quote Originally Posted by unmarkedhelicopter
    Why don't you just use :-

    [vba] Dim sRange As String
    sRange = Application.InputBox("Get Range ", "Title", , , , , , 8).Address
    [/vba]
    i tried that already, but it seems that i cannot select that specifi range which i input....if yes, how??

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by grohm
    Hi Guys

    Basically i just want a range which is not set, but can be defined by 2 inputboxes (one for the (X:y) coordinate and one for the (x:Y) coordinate and then automatically get selected.

    heres my code so far. i just can not define the range through those inpboxes....

    [vba]Dim x As String
    Dim y as String

    x = InputBox("please enter first range coordinate")
    y = InputBox("please enter 2nd range coordinate")

    copyrange = (x:y) 'this is, where it doesnt work)


    With Worksheets("sheet1")
    copyrange.Select
    End With[/vba]

    it must be possible somehow....thanks for having a look, guys!

    cheers

    ben
    Try copyrange = Range(x:y)
    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.

  6. #6
    Quote Originally Posted by johnske
    Try copyrange = Range(x:y)
    doesnt work....

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Outside that snippet of code - have you declared CopyRange As Range?
    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.

  8. #8
    Quote Originally Posted by johnske
    Outside that snippet of code - have you declared CopyRange As Range?

    jep, did that. very strange....

  9. #9
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    ?[VBA]Sub select_range()
    Dim sRange As String
    sRange = Application.InputBox("Get Range ", "Title", Type:=8).Address
    Range(sRange).Select
    End Sub[/VBA]

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    This works[VBA]
    Option Explicit

    Sub try()
    Dim x As String
    Dim y As String
    Dim copyrange As Range
    x = InputBox("please enter first range coordinate")
    y = InputBox("please enter 2nd range coordinate")
    With Worksheets("sheet1")
    Set copyrange = .Range(x, y)
    copyrange.Select
    End With
    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
    ????
    [vba]Dim sRange As String
    sRange = Application.InputBox("Get Range ", "Title", , , , , , 8).Address
    Range(sRange).Select
    [/vba]

    EDIT:
    Oops ! I noticed Charlize has already added the obvious select bit.

    How the hell do you delete a posting here ?
    Last edited by unmarkedhelicopter; 06-05-2007 at 05:26 AM. Reason: noticed Charlize had already done this bit
    2+2=9 ... (My Arithmetic Is Mental)

  12. #12
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    ????
    Quote Originally Posted by unmarkedhelicopter
    ????
    [vba]Dim sRange As String
    sRange = Application.InputBox("Get Range ", "Title", , , , , , 8).Address
    Range(sRange).Select
    [/vba]
    Wants to select the range based on input. This one selects a range. Just not sure it this is exactly what he wants ...

    What do you mean by ????

    ps.: noticed that you altered your post. So discard that ???? Already know what you meant.

  13. #13
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    [vba]Range(Application.InputBox("Get Range ", "Title", , , , , , 2)).Select[/vba]will work also, but I'm guessing grohm's trying to figure out why his original code doesn't work
    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.

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    What format of coordinate are you entering into the Input box?
    A2 or 2,2
    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'

  15. #15
    thanks to all of you guys. it works now (with both versions) with charlizes add unmarked helicopter's solution works and johnsek's works too. Perfect. Thanks!!!!

  16. #16
    so what changed ?
    2+2=9 ... (My Arithmetic Is Mental)

Posting Permissions

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