|
-
Oct 16th, 2016, 11:24 PM
#1
Thread Starter
Addicted Member
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
-
Oct 17th, 2016, 02:47 AM
#2
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
-
Oct 17th, 2016, 12:46 PM
#3
Thread Starter
Addicted Member
Re: Can't applying Union Range from VB
 Originally Posted by westconn1
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.
 Originally Posted by westconn1
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
-
Oct 17th, 2016, 03:16 PM
#4
Re: Can't applying Union Range from VB
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
-
Oct 17th, 2016, 04:04 PM
#5
Thread Starter
Addicted Member
Re: Can't applying Union Range from VB
 Originally Posted by westconn1
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
-
Oct 18th, 2016, 02:14 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|