?
Solved

VB/Excel repetitive use of Range object

Posted on 1998-12-24
4
Medium Priority
?
161 Views
Last Modified: 2010-05-03
Given an Excel worksheet via CreateObject, two Range variables and the following VB5 code fragment within a loop in which x defines a row number (these rows contain sub-totals which are to be charted):
If I = 1 Then
  Set RngAll = Range(.Cells(x, 2), .Cells(x, 14))
Else
  Set RngPart = Range(.Cells(x, 2), .Cells(x, 14))
  Set RngAll = Union(RngPart, RngAll)
End If
(Later RngAll is used as the data source for the chart).
The first time this runs, everything is fine. The second time, even given exactly the same conditions (apparently) it falls over on the first Set RngAll and gives the error "Method 'Range' of object '_Global' failed".
By the way, RngAll and RngPart are set to Nothing after each run and the Excel object is newly created for each run and properly closed afterwards. The application does a lot of other Excel work so References are set.
Can someone offer an explanation and a solution?
0
Comment
Question by:donturnbull
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:donturnbull
ID: 1452345
Edited text of question
0
 
LVL 3

Expert Comment

by:vmano
ID: 1452346
If I > 1, i think you have to set RngAll to something before you can use it in the Set RngAll = Union(RngPart, RngAll) statement.

let me know if this helps
vmano
0
 
LVL 2

Accepted Solution

by:
FlorisMK earned 800 total points
ID: 1452347
The problem lies in using the Range (and Union) methods as global methods, i.e. without explicitly referring to worksheet or application object. Calling them as global methods lets the Automation software create new objects as necessary, and those new objects are not necessarily the same objects you're using.
To solve the problem, let all your method calls refer explicitly back to the objects on which they operate.
So where you call the range object, you should precede it with a period to refer back to the worksheet (as you did with Cells), and where you use Union, you should refer back to the application object you've no doubt created.
The VB5 code below works time after time.

Option Explicit

Public Sub Test()

    Dim ex As Excel.Application
    Dim wb As Excel.Workbook
    Dim ws As Excel.Worksheet
    Dim RngPart As Range, RngAll As Range
    Dim i As Integer, x As Integer

    Set ex = New Excel.Application
    ex.Visible = True

    Set wb = ex.Workbooks.Add
    Set ws = wb.Worksheets(1)

    With ws

        For i = 1 To 10
        x = i
        If i = 1 Then

            'Without the period before "Range", the
            '"_Global" error results
            Set RngAll = .Range(.Cells(x, 2), .Cells(x, 14))

        Else

            'Without the period before "Range", the
            '"_Global" error results the second time
            Set RngPart = .Range(.Cells(x, 2), .Cells(x, 14))

            'Without "ex." before "Union", the
            '"_Global" error results
            Set RngAll = ex.Union(RngPart, RngAll)

        End If

        Next i

    End With
   
    ' without "ex.", the "_Global" error results the second time

    ex.Charts.Add
    'Do stuff with the chart

    Set RngPart = Nothing
    Set RngAll = Nothing
    wb.Close False
    Set ws = Nothing
    ex.Quit
    Set ex = Nothing
   
End Sub

0
 

Author Comment

by:donturnbull
ID: 1452348
Thanks FlorisMK! As it happens I managed to answer my own question after several hours of frustration (and emailed Experts Exchange asking them to refund my points!) but you beat us both to it.
Have a great Christmas!
Don Turnbull
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question