Direct Referencing Non Contiguous Columns

missbanana

New Member
Joined
Nov 25, 2024
Messages
1
Office Version
  1. 365
  2. 2010
  3. 2007
Platform
  1. Windows
Hi all, long time lurker first time poster.
I am somewhat of a VBA newbie and am trying to write a script to assign values from one of my workbooks to a template workbook, however the data in the source file is non contiguous. The below script does not cause an error. however the data from the first column in my source range is copied over to every column in the wb0 range. Wondering if there is an easier way than referencing each column individually? I came across Application.Union but not sure it will work for my situation? Any advice would be most welcome thank you!


Option Explicit
Sub Cavendish_Data()

Application.DisplayAlerts = False

Dim wb0 As Workbook
Dim wb1 As Workbook

Set wb0 = ActiveWorkbook
Set wb1 = Workbooks.Open("C:\HVO\Monthly Dust\Workbook1.xls")

'Assign values from banana daily average outputs to template
LastRow = wb1.Sheets("Block3_1_2").Range("A1").End(xlDown).Row
wb0.Sheets("Daily AVG").Range("H2:H" & LastRow & "," & "I2:I" & LastRow).Value = wb1.Sheets("Block3_1_2").Range("B2:B" & LastRow & "," & "E2:E" & LastRow).Value
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Excel treats a noncontiguous range as an array of values, so you can't assign from a noncontiguous range into another noncontiguous range. You'll have to do each column individually.
 
Upvote 1
Array or Union
Rich (BB code):
Sub Cavendish_Data()
  
    Dim wb0 As Workbook
    Dim wb1 As Workbook
    Dim x
    Set wb0 = ActiveWorkbook

    Set wb1 = Workbooks.Open(ThisWorkbook.Path & "\Workbook1.xls")
    'Set wb1 = Workbooks.Open("C:\HVO\Monthly Dust\Workbook1.xls")
    With wb1.Sheets("Block3_1_2")
        With .Range("a2", .Cells.SpecialCells(11))
            x = Application.Index(.Value, Evaluate("row(1:" & .Rows.Count + 1 & ")"), Array(2, 5)) '<--- Array
            wb0.Sheets("Daily AVG").Range("H2").Resize(UBound(x, 1) - 1, 2) = x   '<--- Output Array
            Union(.Columns(2), .Columns(5)).Copy wb0.Sheets("Daily AVG").Range("H2")  '<--- Union
        End With
    End With
    wb1.Close False
End Sub
 
Upvote 1

Forum statistics

Threads
1,224,118
Messages
6,176,483
Members
452,730
Latest member
palsmith

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