Solved

VB/Excel repetitive use of Range object

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now