Having trouble with vba using range Union method. I have resolved it, at the end. But could not find the proper explanation, why it is so?
Set ws = ThisWorkbook.ActiveSheet
Set rng00 = Range("A1:C3")
Set rng01 = Range("F1:G3")
ws.Union(rng00,rng01).Copy 'the error giving line
When I have changed the line with below one strangely it has started to work. But as I remember I have used union method of range before with ThisWorkbook(or maybe Workbooks(name))
Worksheets("Sheet1").Activate
Application.Union(rng00,rng01).Copy
CodePudding user response:
Union is a function of the Application Object, not of a Worksheet Object. Which means that ws.Union would not work because there is no member with the name Union as a member of ws. The correct parent is Application.Union but since it would be tedious to write Application. for every function, most of the time the Application. is not written.
The arguments of Union must be Range Objects that are from the same Worksheet. When declaring Ranges, it is important to explicitly declare their parent sheet, to avoid issues later with functions like Union. Adding ws in front of Range like ws.Range is how you can declare that the range is a member of that sheet.
So the full corrected code would be
Set ws = ThisWorkbook.ActiveSheet
Set rng00 = ws.Range("A1:C3")
Set rng01 = ws.Range("F1:G3")
Application.Union(rng00, rng01).Copy
CodePudding user response:
Application.Union feat. Qualifying Object References
Your 1st Code
Set ws = ThisWorkbook.ActiveSheet
Set rng00 = Range("A1:C3")
Set rng01 = Range("F1:G3")
ws.Union(rng00,rng01).Copy 'the error giving line
Your 2nd Code
Worksheets("Sheet1").Activate
Application.Union(rng00,rng01).Copy
Application.ThisWorkbook property
ThisWorkbookis (a reference to) the workbook containing this code. It is an exact workbook and there can only be one of it.Dim wb As Workbook: Set wb = ThisWorkbook
Qualifying Worksheets
When referencing the sheets in a workbook, you want to qualify them (note the
wb.):Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")This can be only one worksheet, the worksheet named
Sheet1in thewb- workbook. It can't be the wrong worksheet unless you have incorrectly referenced the workbook.When you do
Set ws = ThisWorkbook.ActiveSheet, it creates a reference to any sheet (worksheet, chart) that is currently active (selected, you're looking at), yet in the lineWorksheets("Sheet1").Activateyou are activatingSheet1˛of theActiveWorkbook, the workbook which is the currently active (could beThisWorkbook, but may not).
Qualifying Ranges
When referencing the ranges in a worksheet, you want to qualify them (note the
ws.):Dim rg1 As Range: Set rg1 = ws.Range("A1:C3") Dim rg2 As Range: Set rg2 = ws.Range("F1:G3")These are ranges in the
ws- worksheet. They can't be the wrong ones unless you have incorrectly referenced the worksheet.When you do
Set rng00 = Range("A1:C3"): Set rng01 = Range("F1:G3"), you are creating references to ranges of theActiveSheet(could be a worksheet, could be a chart), of theActiveWorkbook(could beThisWorkbook, but may not). It would have worked correctly only by chance in case yourActiveWorkbookwasThisWorkbookand yourActiveSheetwas a worksheet namedSheet1in theActiveWorkbook.
Its 'full name' says it all: it's a member of the
Applicationobject, not theWorksheetobject. You can omit theApplication.part from some of theApplicationobject members:Unionis such a member.Union(rng1, rng2).Copy
Alltogether
Option Explicit
Sub CopyRange()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets("Sheet1")
Dim rg1 As Range: Set rg1 = ws.Range("A1:C3")
Dim rg2 As Range: Set rg2 = ws.Range("F1:G3")
Union(rng1, rng2).Copy
End Sub
- Of course, if there is no worksheet named
Sheet1in the workbook, an error will occur. But that's another story.
