Error 1004 with Excel VBA Union

Jon R

New Member
Joined
Oct 6, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

The following is a code extract causing the issue:


VBA Code:
  ' WBData is the Excel workbook object and WSData the worksheet object in the workbook
  ' Set the 2 ranges (no issues here)
  Set HdrTblRange = WSData.Range(WSData.Cells(cs_Row_Headers, 1), WSData.Cells(cs_Row_Headers, cs_Col_FCSubmission))
  Set DataTblRange = WSData.Range(WSData.Cells(DRow + cs_Row_Headers, 1), WSData.Cells(DRow + cs_Row_Headers, cs_Col_FCSubmission))
  
  ' Give each range a name (no issues either)
  WBData.Names.Add "FCHdr", HdrTblRange
  WBData.Names.Add "FCData", DataTblRange
  
  ' The following statements result in a "Runtime error 1004_ Method Union of Object _Global failed"
  Set UnionRange = Union(HdrTblRange, DataTblRange)
  Set UnionRange = Union(WSData.Range("FCHdr"), WSData.Range("FCData"))

I can use the ranges without any issues directly or via their names. However the Union statements both result in ""Runtime error 1004_ Method Union of Object _Global failed".

Does anyone have any ideas?

Thanks
Jon
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
That code runs fine for me in 2016 and 365.
 
Upvote 0
Thanks Rory.

Unfortunately no success. Have built a work around.
 
Upvote 0
Care to share in case it helps anyone else in the future?
 
Upvote 0
Please do not mark a post as the solution if you have not provided the solution!
If you post your actual solution, then you can mark that post as the solution.
 
Upvote 0
Sure - the workaround was:

1) To create a temporary worksheet
2) Copy the first required range from the source to the temp worksheet
3) Copy the second required range from the source to the temp worksheet
4) Set a normal Range (not a Union) on the temp worksheet
5) Process Range as normal

Not pretty but it works!
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top