[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

VB/Excel repetitive use of Range object

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
donturnbull
Asked:
donturnbull
  • 2
1 Solution
 
donturnbullAuthor Commented:
Edited text of question
0
 
vmanoCommented:
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
 
FlorisMKCommented:
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
 
donturnbullAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now