Consulting

Results 1 to 11 of 11

Thread: Defining sourcerange

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location

    Exclamation Defining sourcerange

    Could someone perhaps help with the correct code to select a specific range?

    Set SourceRange = Range(ListBox2.RowSource).Range("A2:J2")

    Thx,

    Jaypee

  2. #2
    In case SourceRange is a Range type variable, the code you gave is correct in syntax. (Whether or not it is the range you want to define, I don't know.)
    But you can only select this range if the related worksheet is active.

    Jimmy
    -------------------------------------------------
    The more details you give, the easier it is to understand your question. Don't save the effort, tell us twice rather than not at all. The amount of info you give strongly influences the quality of answer, and also how fast you get it.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    So if Jimmy's answer is not it, what is the problem you are getting?
    ____________________________________________
    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

  4. #4
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location
    Hi xld, this is the code I am using. I saved and closed my worksheet and when starting off again, the problem is solved. The problem now is that it seems not to select that particular sheet's range. Here is some of that code;

    worksheets(TextBox29.Value).Select
    Dim SourceRange As Excel.Range
    Dim Val1 As String, Val2 As String, Val3 As String, Val4 As String, Val5 As String, Val6 As String, _
    Val7 As String, Val8 As String
    If (ListBox2.RowSource <> vbNullString) Then
    Set SourceRange = Range(ListBox2.RowSource).Range("A2:J2")
    Else
    Set SourceRange = Range("A2:C2")
    Exit Sub
    End If

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What is in the ListBox and what is in ListBox2.RowSource?

    Can't you post the workbook, we are groping in the dark here.
    ____________________________________________
    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

  6. #6
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location

    Defining range

    The further I go the more in trouble I seem to get!
    Attached is the file as requested. What I am trying to accomplish is to when I select multipage1 you'll see that it selects a sheet as well
    Currently I cannot get listbox2 to read from that sheet.

    Hope this makes sense!

    Thx.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Need more help mate, I have no idea what that is doing or is supposed to do (far too much code for me to debug).

    Layout in step deatil what you do, and what happens correctly and incorrectly.

    ALthough my bet is that it is going to be because you don't qualify the ranges with the appropriate sheet objects, you are relying on the right one being active.
    ____________________________________________
    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

  8. #8
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location
    You're a brave man just to open that file!!

    I'll break it down into another worksheet and repost it!

    Thanks so far!

  9. #9
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location
    xld, I got this code running, thanx.
    On the same subject : I am struggeling a bit to get the following working properly. Is there perhaps anything you could see off hand that could cause a problem?

    Set SourceRange = Range(ListBox2.RowSource).worksheets(TextBox29.Value).Range("A2:H2")

    Thx in advance, J

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, worksheets is not a roperty of range, it is the other way around, so you can't do

    Range(...).Worksheets(...)

    You can do

    Worksheets(...).Range(...)

    so you probably mean

    [vba]

    Set SourceRange = worksheets(TextBox29.Value).Range(ListBox2.RowSource).Range("A2:H2")
    [/vba]
    ____________________________________________
    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

  11. #11
    VBAX Regular
    Joined
    Nov 2007
    Posts
    11
    Location
    Hi xld, makes sense hey? An object defined error pops up on that line. This is the code I have used;

    Dim SourceRange As Excel.Range
    Dim Val1 As String, Val2 As String, Val3 As String, Val4 As String, Val5 As String, Val6 As String, _
    Val7 As String, Val8 As String
    If (ListBox2.RowSource <> vbNullString) Then
    Set SourceRange = worksheets(TextBox29.Value).Range(ListBox2.RowSource).Range("A2:H2")
    Else
    Set SourceRange = worksheets(TextBox29.Value).Range("A2:C2")
    Exit Sub
    End If

    Could it be that I have not defined something somwhere?

    Thx, J

Posting Permissions

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