Combine headers from two sheets onto a third

JimSnyder

Board Regular
Joined
Feb 28, 2011
Messages
125
This is using Excel 2013 and VBA. The goal of the project is to take data from sheet A and find matches on either sheet B or sheet C and build sheet D or sheet E based on matches between A+B or A+C. The use of match and looping are taking care of the data, but I need to combine the headers.

Essentially, I find a match on sheet B from sheet A and build sheet D with the headers of sheet A and the headers of sheet B. I am attempting to use application.union to combine the two ranges of headers onto sheet D. I am getting an error with the union (1003).

Here is the sample code:
Code:
    Set wkbk = Workbooks.Open(filePath)
    wkbk.Activate
    With wkbk
        Set sourceSheet = .Sheets(srcSheet)
        Set targSheet1 = .Sheets(trgSheet1)
        Set targSheet2 = .Sheets(trgSheet2)
        
        ' Get size of rows to be copied to set destination range
        Set sourceAllRange = sourceSheet.Range(allBegin & 1, allEnd & 1)
        Set sourceFAR130Range = targSheet1.Range(far130Begin & 1, far130End & 1)
        Set sourceFAR130ORange = targSheet2.Range(far130OBegin & 1, far130OEnd & 1)
        
        ' Get number of columns of each sheet
        colAllCount = sourceAllRange.Columns.Count
        col130Count = sourceFAR130Range.Columns.Count
        col130OCount = sourceFAR130ORange.Columns.Count
        
        ' Add a sheet for matched FAR130 values
        .Worksheets.Add After:=.Worksheets(Worksheets.Count)
        Set tabFAR130 = .Worksheets(Worksheets.Count)
        tabFAR130.Name = moveSheet1
        
        ' Copy All and FAR130 headers to new sheet
        With tabFAR130
            Set dest130Range = .Cells(1, colAllCount + col130Count)
[COLOR=#ff0000]            Set dest130Range = Application.Union(sourceAllRange, sourceFAR130Range)[/COLOR]
        End With

I can add the variable definitions if needed. Note that dest130Range, sourceAllRange, and sourceFAR130Range are all declared as ranges.
 

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.
I don't think you can use Application.Union to form a union among ranges on different sheets.
 
Upvote 0
Thanks, JoMoe. I finally used brute force:
Code:
        ' Copy All and FAR130 headers to new sheet
        With tabFAR130
            sourceSheet.Range(allBegin & 1, allEnd & 1).Copy Destination:=.Range(allBegin & 1, allEnd & 1)
            targSheet1.Range(far130Begin & 1, far130End & 1).Copy Destination:=.Range(pasteBegin & 1)
        End With
 
Upvote 0

Forum statistics

Threads
1,216,025
Messages
6,128,336
Members
449,443
Latest member
Chrissy_M

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