Solved

VB/Excel repetitive use of Range object

Posted on 1998-12-24
4
158 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
  • 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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pull multiple cvs files into one access table 28 69
MsgBox 2 59
Prevent user closing word document opened with VB6 6 78
Create Files based on Cell Values in a Range in Excel 12 43
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

828 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