Consulting

Results 1 to 14 of 14

Thread: Solved: Set Range to a spreadsheet

  1. #1
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location

    Solved: Set Range to a spreadsheet

    Hi, Currently im learning to use the spreadsheet inside a userform but when I try to set a range variable to the sheet I get an error.

    dim rangex as range
    set rangex = myspreadsheet.sheets(1).range("A1")

    Currently Im working with dim rangex as variant (which i usually do when I give up) and works but i was hoping some one could give me the correct type. thanks

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi there,

    The correct type is Range, as you had it. The question is why doesn't it work. Is that workbook open? Is that the project name for the workbook? If not, you'll need to do it like this ..

    [vba]Dim RangeX as Range
    Set RangeX = Workbooks("myspreadsheet").Sheets(1).Range("A1")[/vba]

    Does that help?

  3. #3
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    No it does not, it pops the error (-2147352565 (8002000b)). Not sure if that is of any use.

  4. #4
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by makako
    Hi, Currently im learning to use the spreadsheet inside a userform but when I try to set a range variable to the sheet I get an error.

    dim rangex as range
    set rangex = myspreadsheet.sheets(1).range("A1")

    Currently Im working with dim rangex as variant (which i usually do when I give up) and works but i was hoping some one could give me the correct type. thanks
    Range is the correct type. This sounds like an OWC (Office Web Component) spreadsheet - is it? If so, AFAIK there can only ever be one sheet, so sheets(1) is redundant and will be causing an error. I assume you've renamed Spreadsheet1 (the default name) to myspreadsheet, so try
    [vba]
    dim rangex as range
    set rangex = myspreadsheet.range("A1")
    [/vba]there is a very extensive Help file available for OWC, click on the spreadsheet in the VBE window and press F1 to view it...
    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.

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    It it's not an OWC by chance, the only other explanation I would have is that either the file is not open or the first sheet is not a worksheet but maybe a chart sheet.

  6. #6
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    I have more than 1 sheet, the control I added is microsoft office spreadsheet 11. But I would gladly change it to any other object if its easier to use.

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    So this is a standard workbook? Can you upload an example/copy of it?

  8. #8
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location

    Example

    Example

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    After reading the OWC Help files it seems that the use of the 'Set' keyword is not supported for ranges on the OWC spreadsheet, so your code (below) will fail...[VBA]Private Sub Button_Click()
    Dim RangeX As Range, RangeY As Range
    Set RangeY = Range("A1")
    Set RangeX = Spreadsheet1.Sheets(1).Range("A1")
    RangeY.Copy RangeX
    End Sub[/VBA]However, this will work...[VBA]Private Sub Button_Click()
    Range("A1").Copy
    Spreadsheet1.ActiveSheet.Range("A1").Paste
    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.

  10. #10
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    ok, this can work with the copy example but using a variable ( to simplify the handeling ) has to be declared as variant?

  11. #11
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by makako
    ok, this can work with the copy example but using a variable ( to simplify the handeling ) has to be declared as variant?
    Personally, I'd avoid using variants unless absolutely essential because they're too memory-intensive and slow.

    With the code you have, a variable isn't really necessary. I don't see any point in cluttering up a procedure by declaring a variable that's only going to be used once, in that case it's better to refer directly to whatever it is that you're handling (if the variable's only for you to clarify what you're doing it's better to use comments). However if you want to use variables you can use RangeX as type Object... [VBA]Private Sub Button_Click()

    Dim RangeX As Object, RangeY As Range

    Set RangeY = Range("A1")
    Set RangeX = Spreadsheet1.ActiveSheet.Range("A1")

    RangeY.Copy
    RangeX.Paste

    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.

  12. #12
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    I get that but what I have posted here is just an example, the complete code is much longer and is part of a 3 MB zip addin that Im unable to post. But, is object a better way to decalre than variant? when can I use it. thanks

  13. #13
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Variant is too broad and covers too many things, Object narrows down the field a bit more but I'd really only use it in instances such as the OWC spreadsheet where you 'have to' - Range is the preferred type
    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
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    Thanks, i used it and work fine.

Posting Permissions

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