Results 1 to 6 of 6

Thread: Can't applying Union Range from VB

  1. #1

    Thread Starter
    Addicted Member sergeos's Avatar
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    167

    Can't applying Union Range from VB

    Hi,
    i'm need combine several Ranges in one Range. So, code in VBA is working:
    Code:
    sAddress = "A4:R9,A11:R436,A439:R452,A454:R1326"
    Set oRanges = Application.Range(sAddress)
    all ok.

    And, when i'm trying running code like this, the VB6.0 IDE generate error. My code is:
    Code:
    sAddress = "A4:R9,A11:R436,A439:R452,A454:R1326"
    Set oRanges = oExcel.Range(sAddress)
    If I write by one Range
    Code:
    sAddress = "A4:R9" 'or for example "A11:R436"
    Set oRanges = oExcel.Range(sAddress)
    all work.

    Is it possible use anyway non-continues Ranges?
    Ten Years After - 01 You Give Me Loving

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Can't applying Union Range from VB

    a range is a member of a worksheet, not the application, while you may get away with it in vba, or even in vb6 if there is currently an activesheet

    this will not work
    Code:
    Set oexcel = CreateObject("excel.application")
    sAddress = "A4:R9,A11:R436,A439:R452,A454:R1326"
    Set oRanges = oexcel.Range(sAddress)
    because there is no activesheet, in fact no open workbook

    you should always specify which sheet you want to work with
    Code:
    Set oRanges = oexcel.workbooks("mybook.xls"),sheets("sheet1").Range(sAddress)
    or use object variables for your workbooks and sheets

    as you have only posted very limited code, i can only guess that this is probably your problem
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member sergeos's Avatar
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    167

    Re: Can't applying Union Range from VB

    Quote Originally Posted by westconn1 View Post
    a range is a member of a worksheet, not the application, while you may get away with it in vba, or even in vb6 if there is currently an activesheet

    this will not work
    Code:
    Set oexcel = CreateObject("excel.application")
    sAddress = "A4:R9,A11:R436,A439:R452,A454:R1326"
    Set oRanges = oexcel.Range(sAddress)
    because there is no activesheet, in fact no open workbook
    This code work fine in VBA instance. In VB only with continued Range. In fact I not need activesheet, i'm define virtual Range from Application methods.

    Quote Originally Posted by westconn1 View Post
    you should always specify which sheet you want to work with
    Code:
    Set oRanges = oexcel.workbooks("mybook.xls"),sheets("sheet1").Range(sAddress)
    or use object variables for your workbooks and sheets

    as you have only posted very limited code, i can only guess that this is probably your problem
    Ok, sorry for this, please, see my example code
    Code:
    Private Sub test()
        Dim objExcel        As Object
        Dim objWorkbook     As Object
        Dim oRanges
        
        Dim sAddress         As String
        
        Set objExcel = CreateObject("Excel.Application")
        Set objWorkbook = objExcel.Workbooks.Open("c:\test.xls")
        
        ExcelWorkSheet = objWorkbook.Worksheets(1)
        objExcel.Visible = True
        
        sAddress = "A4:R436"
        Set oRanges = ExcelWorkSheet.Range(sAddress)       '<= work as worksheet member
        '(Set oRanges = objExcel.Range(sAddress)       '<= also work as application member)
        
        sAddress = "A4:R9,A11:R436,A439:R452,A454:R1326"
        Set oRanges = ExcelWorkSheet.Range(sAddress)      'ERROR Can't assign non-continues Ranges. In VBA code this part work correctly.
        
        Set objWorkbook = Nothing
        Set objExcel = Nothing
        Set oRange = Nothing
    
    End Sub
    Any ideas?
    Last edited by sergeos; Oct 17th, 2016 at 12:51 PM.
    Ten Years After - 01 You Give Me Loving

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Can't applying Union Range from VB

    Any ideas?
    i pasted your code, changed file name, added set keyword for excelworksheet

    all worked, without error for me

    i retested in a different version of windows and excel
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member sergeos's Avatar
    Join Date
    Apr 2009
    Location
    Belarus
    Posts
    167

    Re: Can't applying Union Range from VB

    Quote Originally Posted by westconn1 View Post
    i pasted your code, changed file name, added set keyword for excelworksheet

    all worked, without error for me

    i retested in a different version of windows and excel
    My problem still in:
    And, when i'm trying running code like this, the VB6.0 IDE generate error.
    Did you try in Visual Basic 6.0 IDE?
    Ten Years After - 01 You Give Me Loving

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Can't applying Union Range from VB

    Did you try in Visual Basic 6.0 IDE?
    yes on 2 computers
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width