How to reference a union range in Access VBA

From MS Access vba I am trying to generate a chart from excel sheet.  The DataSource for the chart is a non-contiguous range of cells.  When using the Union method on the non-contiguous ranges I receive the Method 'Union' objected _global failed.

Here is the line that is failing:
Set rngCountOfAnsCol = Union(WS.Range("C$12:$C$13"), WS.Range("$E$12:$E$13"), WS.Range("$G$12:$G$13"))

I have tried WB.Union(WS.Range...

I still get errors.
Who is Participating?
kkhan7Connect With a Mentor Author Commented:

You are the Man!!!
Yeah  WS.Application.Union  processed perfectly -- NO Error !!!  Thank you sooo much. ;o)

How did you come up with that?  Can you point me to a book or resource?  If it's not too much trouble.

Now the chart is not showing all the items in the legend.  Should I increase chart size?  Or is there some other setting/code I must use?
Gustav BrockCIOCommented:
You can't do it this - lazy - way. You have to be extremely specific when defining and accessing Excel objects - like this example:

  ' Prefix for Named Ranges (DFI groups).
  Const cstrDFIRangePrefix  As String = "DFIGruppe"
  ' Count of Names Ranges (DFI groups).
  Const cbytDFIRangeCount   As Byte = 30
  ' Column numbers.
  Const cbytDFIArt          As Byte = 1
  Const cbytDFIBudget       As Byte = 8
  Const cbytDFIRealiseret   As Byte = cbytDFIBudget + 1
  Const cbytDFIRest         As Byte = cbytDFIBudget + 2
  Const cbytDFITotal        As Byte = cbytDFIBudget + 3
  Const cbytDFIDifference   As Byte = cbytDFIBudget + 4
  Dim wkb                   As Workbook
  Dim wks                   As Worksheet
  Dim wksData               As Worksheet
  Dim rng                   As Range
  Dim rngDFI                As Range
  Dim rngArea               As Range
  Dim alngValues(cbytDFIBudget To cbytDFIDifference)  As Long
  Dim lngRows               As Long
  Dim lngColumns            As Long
  Dim bytN                  As Byte
  Dim strDFIRangeSuffix     As String
  Dim booError              As Boolean
  Dim intDFIArtSidste       As Integer

  Set wkb = ActiveWorkbook()
  Set wks = wkb.Worksheets(cstrWorksheetBudgetName)
  ' Union Named Ranges to one Named Range.
  With wks
    For bytN = 1 To cbytDFIRangeCount
      strDFIRangeSuffix = Format(bytN, "00")
      If bytN = 1 Then
        Set rngDFI = .Range(cstrDFIRangePrefix & strDFIRangeSuffix)
        Set rngDFI = Union(rngDFI, .Range(cstrDFIRangePrefix & strDFIRangeSuffix))
      End If
  End With
  Set wksData = wkb.Worksheets(cstrWorksheetDataName)
  Set rng = wksData.Range("A1")

Also be very careful closing all objects in reverse order before you quit Excel.

I think the poster is trying to do this from Access via automation code.
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Rory ArchibaldCommented:
Why not just use:

Set rngCountOfAnsCol = WS.Range("C12:C13,E12:E13,G12:G13")

Open in new window

Gustav BrockCIOCommented:
Yes Peter, missed that, sorry.

Here is how to control Excel from Access VBA given a reference is set to:

    Microsoft Excel 14.0 Object Library

Public Sub RenameWorkSheet()

    Dim xls     As Excel.Application
    Dim wkb     As Excel.Workbook
    Dim wks     As Excel.Worksheet
    Dim rng     As Excel.Range
    Set xls = New Excel.Application
    Set wkb = xls.Workbooks.Open("c:\test\workbook1.xlsx")
    Set wks = wkb.Worksheets(1)
    wks.Name = "My New Name"

    ' Insert range code here.
    Set rng = wks.Range(....)
    ' ---------------------------------

    wkb.Close True
    Set rng = Nothing
    Set wks = Nothing
    Set wkb = Nothing
    Set xls = Nothing

End Sub

Open in new window

kkhan7Author Commented:
Hello gustav,

The VBA code you provided doesn't include the Union method.   My problem is that from Access VBA the Union method is not working.  

Do you have any other suggestions?
Gustav BrockCIOCommented:
Yes, too easy: Excel.Union(...)

Rory ArchibaldCommented:
You don't need union at all here - see my earlier post. ;)
kkhan7Author Commented:
Hello Rorya,

Yes, on my own I am using currently as a work around to this problem.  I was really hoping to use the Union method.  

Set rngCountOfAnsCol = WS.Range("C12:C13,E12:E13,G12:G13")

The new problem with using the above range the dataseries for the chart has to be set the non-contiguous range as well.  How is that set?  The dataseries is incomplete.  See attached file, tab called Grade_10_chart.

Thanks in advance. ;o)
kkhan7Author Commented:


gives me Method 'Union' object failed _global  err

Do you know why?  Have you tried using it in MS Access 2010 vba to control MS Excel 2010?  Do you have another suggestion?

Thanks in advance :o)
kkhan7Author Commented:

Attached is the code I am currently using.
Rory ArchibaldCommented:
If you must use Union (which will give you the same outcome as the method I suggested) you need:
WS.Application.Union(WS.Range("C$12:$C$13"), WS.Range("$E$12:$E$13"), WS.Range("$G$12:$G$13"))

Open in new window

Your sample workbook contains pivot charts - you cannot set the range for those; they always use the source pivot table data.
kkhan7Author Commented:

Now the chart is not showing all the items in the legend.  Should I increase chart size?  Or is there some other setting/code I must use?

Legend should show grades 6 - 13
Gustav BrockCIOCommented:
You are looking for trouble here:

Set WB = GetObject(sFile)
Set WS = WB.Sheets(shtPivotTable)

Set WB as the declaration as in my demo code.
Also, Sheets are not WorkSheets.

You may get it to work without these steps using strange and messy code, but you may just as well end up with Excel not closing completely.
Did you check the Task Manager that Excel Closes?

kkhan7Author Commented:

What would be the best way to generate a chart for each grade in the resulting pivot table for an non-contiguous data range  or for only the columns called "Count of Answers Received"?

Do I need to create a new sheet for each grade that contains a simple contiguous range of data and then under it the chart?

How do I give more points for this answer?  After you have answered it successfully.
kkhan7Author Commented:
Thanks Gustav,

I'll check out your suggestions.
kkhan7Author Commented:
The error Method 'Union' object _global failed didn't occur when I used WS.Application.Union(...  as suggested in the MS Access 2010 vba code to control Excel objects.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.