Solved

VB/Excel repetitive use of Range object

Posted on 1998-12-24
4
157 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA color chart bars 12 86
JSON Response and request in VB6 application 11 421
VBA filters 2 61
Recommendation vb6 to vb.net or others 14 149
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

777 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