A Brief Study
Copy Values, Formats, Formulas
Sub NonContiguousRow()
Dim iT As Long
iT = 1
Dim cols As Range
Set cols = Sheet1.Range("B:C,E:M")
' Optionally:
'Set cols = Union(Sheet1.Columns("B:C"), Sheet1.Columns("E:M"))
Dim rRng As Range
Set rRng = Intersect(Sheet1.Rows(iT), cols)
rRng.Copy Sheet2.Cells(1, "A")
' This will also work:
'Dim ColumnsCount As Long
'ColumnsCount = getColumnsCount(cols)
'rRng.Copy Sheet2.Cells(1, "A").Resize(, ColumnsCount)
' This will NOT work:
'Sheet2.Cells(1, "A").Resize(, ColumnsCount).Value = rRng.Value
End Sub
Function getColumnsCount( _
aRange As Range) _
As Long
If Not aRange Is Nothing Then
Dim rng As Range
For Each rng In aRange.Areas
getColumnsCount = getColumnsCount + rng.Columns.Count
Next rng
End If
End Function
Copy Values
Sub TESTgetRow()
Dim iT As Long
iT = 1
Dim cols As Range
Set cols = Sheet1.Range("B:C,E:M")
Dim Data As Variant
Data = getRow(cols, iT)
Sheet2.Cells(1, "A").Resize(, UBound(Data) - LBound(Data) + 1).Value = Data
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose: Returns the values at the intersection of a range
' and one of its worsheet's rows, in an array.
' Remarks: Supports non-contiguous ranges.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function getRow( _
aRange As Range, _
Optional ByVal aRow As Long = 1) _
As Variant
If Not aRange Is Nothing Then
Dim rRng As Range
Set rRng = Intersect(aRange, aRange.Worksheet.Rows(aRow))
If Not rRng Is Nothing Then
With CreateObject("Scripting.Dictionary")
Dim rng As Range
Dim cel As Range
Dim n As Long
For Each rng In rRng.Areas
For Each cel In rng.Cells
n = n + 1
.Item(n) = cel.Value
Next cel
Next rng
getRow = .Items
End With
Else
' Row range is empty ('Nothing').
End If
Else
' Range is empty ('Nothing').
End If
End Function