Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to move the contents of a cell


I am trying to figure out how to write a macro to take the contents o
cells which are across a few columns and move them all into one lon
column.

Example:

|column1| |column2| |column3|
|dog.......| |bird.......| |cat.........|
|car........| |truck.....| |bus........|

Into

|column4|
dog
bird
cat
car
truck
bus

It seems like it shouldn't be too hard, I am very new to Macros, so an
help would be greatly appreciated.

Thanks,
Je

--
JenBasc
-----------------------------------------------------------------------
JenBasch's Profile: http://www.excelforum.com/member.php...fo&userid=2736
View this thread: http://www.excelforum.com/showthread.php?threadid=46903

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Macro to move the contents of a cell

If you have the cells selected then this should work:

Public Sub oneColumn()

Dim targetCell As Range
Dim i As Integer

'Just where we want the list to go
Set targetCell = Range("A1")
i = 1

For Each cl In Selection.Cells
targetCell.Cells(i, 1) = cl.Value
i = i + 1
Next

End Sub


Of couse this will lead to problems if you want to put the information where
your original data is. In that case it's a bit more complicated; you'll need
to put you cells in an array and copy it back on to the sheet:


Public Sub oneColumn()

Dim targetCell As Range
Dim numberOfCells As Integer
Dim source() As Variant
Dim i As Integer

'The first cell in the selection
Set targetCell = Selection.Cells(1, 1)

'Count the nunber of cells.
numberOfCells = Selection.Rows.Count * Selection.Columns.Count

'Redefine our array to be big enough
ReDim source(numberOfCells)

i = 1

'loop through each cell and remember what's in it
For Each cl In Selection.Cells
source(i) = cl.Value
cl.Value = ""
i = i + 1
Next

'Output our original cell at the target
For i = 1 To numberOfCells
targetCell.Cells(i, 1) = source(i)
Next

End Sub

Both of these procedures assume that you have your source rows highlighted.



- Rm





"JenBasch" wrote:


I am trying to figure out how to write a macro to take the contents of
cells which are across a few columns and move them all into one long
column.

Example:

|column1| |column2| |column3|
|dog.......| |bird.......| |cat.........|
|car........| |truck.....| |bus........|

Into

|column4|
dog
bird
cat
car
truck
bus

It seems like it shouldn't be too hard, I am very new to Macros, so any
help would be greatly appreciated.

Thanks,
Jen


--
JenBasch
------------------------------------------------------------------------
JenBasch's Profile: http://www.excelforum.com/member.php...o&userid=27369
View this thread: http://www.excelforum.com/showthread...hreadid=469032


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
Move cell contents with macro Peridot Excel Discussion (Misc queries) 4 September 30th 09 03:35 PM
macro to move part of cell contents to another cell icetoad hisself Excel Discussion (Misc queries) 4 November 27th 06 07:19 PM
Macro to remove contents of cell and move all other contents up one row adw223 Excel Discussion (Misc queries) 1 July 1st 05 03:57 PM
Macro to move selected cell contents to a specific column on same row Ben Johnson[_3_] Excel Programming 1 February 10th 04 08:43 PM
macro to move contents of directory Harvey[_3_] Excel Programming 0 February 9th 04 07:36 PM


All times are GMT +1. The time now is 12:25 PM.

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"