Copy .Areas.Item(1,2,3,4,5, etc.) to a column or a row
"Howard" wrote:
This little snippet does a good job of taking the non-contiguous
selected cells in named range "Fivex" and putting them in the same
address on sheet 2.
How can I take the non-contiguous selected cells and list them in
a column OR a row?
One way....
Option Explicit
Sub doit()
Dim src As Range, dst As Range
Dim n As Long
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set dst = Sheet2.Range("a2")
n = 0
For Each src In Range("fivex")
n = n + 1
src.Copy dst(n)
Next
dst(n).EntireColumn.AutoFit ' optional
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
But copy-and-paste is needed only if you want to copy formats as well as
values.
Also, beware that when copy-and-pasting formulas, Excel might try to change
them. The result might not be copacetic with their new arrangement.
If you just want to copy values, replace ``src.Copy dst(n)`` with
dst(n)=src.
If you want to copy values and just numeric formats (not also conditional
formats, for example), replace ``src.Copy dst(n)`` with:
With dst(n)
.Value = src
.NumberFormat = src.NumberFormat
End With
Alternatively, the following copies just values more quickly.
Option Explicit
Sub doit2()
Dim src As Range, dst As Range
Dim n As Long
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set dst = Sheet2.Range("a2")
ReDim v(1 To Range("fivex").Count, 1 To 1)
n = 0
For Each src In Range("fivex")
n = n + 1
v(n, 1) = src
Next
With dst
.Resize(n) = v
.EntireColumn.AutoFit
End With
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
|