Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Copy .Areas.Item(1,2,3,4,5, etc.) to a column or a row

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?

The commented out line renders the last selected cell in the area to the range M1.

Regards,
Howard

Option Explicit

Sub copyrng()
Dim i As Long
Dim Fivex As Range
With ActiveSheet.Range("Fivex")
For i = 1 To .Areas.Count
'.Areas.Item(i).Copy Sheets("sheet2").Range("M1")
.Areas(i).Copy Sheets("sheet2").Range(.Areas(i).Address)
Next
End With
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Copy .Areas.Item(1,2,3,4,5, etc.) to a column or a row

Basically, you need to loop each area and put the value into an output
array that you can dump back into your sheet to a row or col as
desired.

Try...

Sub CopyAreas()
Dim vAreas, vData, n&, j&, sVals$
vAreas = Split(Selection.Address, ",")
For n = LBound(vAreas) To UBound(vAreas)
vData = Range(vAreas(n))
If Not IsArray(vData) Then '//single cell
sVals = sVals & "~" & vData
Else
For j = LBound(vData) To UBound(vData)
sVals = sVals & "~" & vData(j, 1)
Next 'j
End If 'Not IsArray(vData)
Next 'n
vData = Split(Mid(sVals, 2), "~")
'Resize the target range and dump the data
'To col
Range("M1").Resize(UBound(vData) + 1, 1) = _
Application.Transpose(vData)
'To row
Range("M1").Resize(1, UBound(vData) + 1) = vData
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Copy .Areas.Item(1,2,3,4,5, etc.) to a column or a row

On Thursday, July 18, 2013 11:08:33 PM UTC-7, GS wrote:
Basically, you need to loop each area and put the value into an output

array that you can dump back into your sheet to a row or col as

desired.



Try...



Sub CopyAreas()

Dim vAreas, vData, n&, j&, sVals$

vAreas = Split(Selection.Address, ",")

For n = LBound(vAreas) To UBound(vAreas)

vData = Range(vAreas(n))

If Not IsArray(vData) Then '//single cell

sVals = sVals & "~" & vData

Else

For j = LBound(vData) To UBound(vData)

sVals = sVals & "~" & vData(j, 1)

Next 'j

End If 'Not IsArray(vData)

Next 'n

vData = Split(Mid(sVals, 2), "~")

'Resize the target range and dump the data

'To col

Range("M1").Resize(UBound(vData) + 1, 1) = _

Application.Transpose(vData)

'To row

Range("M1").Resize(1, UBound(vData) + 1) = vData

End Sub



--

Garry



Thanks, Garry.

Pretty slick!

Appreciate it.

Regards,
Howard


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 536
Default Copy .Areas.Item(1,2,3,4,5, etc.) to a column or a row

On Thursday, July 18, 2013 11:45:52 PM UTC-7, joeu2004 wrote:
"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


Thanks joeu2004.

Works well also, I will play with the options you submited.

Many thanks.

Regards,
Howard


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Copy .Areas.Item(1,2,3,4,5, etc.) to a column or a row

Note that my suggestion is only an example of how to deal with areas
individually, assuming selection in individual cols. It would need to
be modified slightly to deal with a defined name non-contiguous range
so it works with its area addresses rather than selection address.

Also, provision must be added to include multi-col areas if
UBound(vData, 2) is greater than 1!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy Multiple Areas from One Workbook to Another jycpooh Excel Programming 4 October 11th 11 09:14 PM
Find item in column Copy to new location Paul Morgan Excel Programming 2 December 1st 10 02:25 PM
To find rate of each item from item.xls and to copy price.xls pol Excel Discussion (Misc queries) 7 July 16th 09 12:49 AM
What are the gray areas outside row and column identifiers? Kim Excel Discussion (Misc queries) 3 December 28th 06 07:17 PM
Copy & paste in multiple areas using VBA Rob Excel Discussion (Misc queries) 12 April 11th 05 02:09 PM


All times are GMT +1. The time now is 07:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"