Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default combine columns

I want to combine columns
1st column is
Tom
Dick
Harry

2nd column is
george
1
5

3rd column is
hhh
ajjas
jkajd

The result would be all spreadsheet data in one column
Tom
Dick
Harry
george
1
5
hhh
ajjas
jkajd

Mary

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default combine columns


Hello Tom,

This will combine data in the range of "A1:C25" ( you can change thi
to match your needs) and overwrite the data in column "A" as a singl
column.


Code
-------------------

Sub CombineColumns()

Dim Cell
Dim I As Long
Dim Rng As Range
Dim Temp()

ReDim Temp(0)
Set Rng = ActiveSheet.Range("A1:C25")

For Each Cell In Rng
I = I + 1
ReDim Preserve Temp(I)
Temp(I) = Cell
Next Cell

Rng.ClearContents
For I = 1 To UBound(Temp, 1)
ActiveSheet.Cells(I, "A").Value = Temp(I)
Next I

End Sub

-------------------

--
Leith Ros

-----------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...fo&userid=1846
View this thread: http://www.excelforum.com/showthread.php?threadid=49721

  #3   Report Post  
Posted to microsoft.public.excel.programming
Max Max is offline
external usenet poster
 
Posts: 9,221
Default combine columns

Tried out your sub on the OP's data, Leith and I got:

Tom
george
hhh
Dick
1
ajjas
Harry
5
jkajd

But the OP wanted it as:

Tom
Dick
Harry
george
1
5
hhh
ajjas
jkajd


How could your sub be amended to provide the OP's result? Thanks.
--
Rgds
Max
xl 97
---
Singapore, GMT+8
xdemechanik
http://savefile.com/projects/236895
--


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default combine columns

This uses row 1 to determine how many columns to put at the bottom of column A.

Since it cleans up those other columns, test this against a copy of your data
(or close without saving):

Option Explicit
Sub testme01()

Dim wks As Worksheet
Dim RngToCopy As Range
Dim DestCell As Range
Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long

Set wks = Worksheets("sheet1")

With wks
FirstCol = 2
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

For iCol = FirstCol To LastCol
Set RngToCopy = .Range(.Cells(1, iCol), _
.Cells(.Rows.Count, iCol).End(xlUp))
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)

DestCell.Resize(RngToCopy.Rows.Count, 1).Value _
= RngToCopy.Value
Next iCol

.Range(.Columns(FirstCol), .Columns(LastCol)).Delete

End With

End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

wrote:

I want to combine columns
1st column is
Tom
Dick
Harry

2nd column is
george
1
5

3rd column is
hhh
ajjas
jkajd

The result would be all spreadsheet data in one column
Tom
Dick
Harry
george
1
5
hhh
ajjas
jkajd

Mary


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default combine columns

Thankyou
The procedure for combining the columns worked as you said. I might
also have blank cells within the range that is combined into the single
column. I need to maintain the value of those cells (empty) when the
columns are combined into one. How do you account for empty cells and
keep them as empty in the single column?
Mary



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default combine columns

That code copies each column from row 1 to the last used cell in that column.

Can you specify a column that defines the last used row?

I used column A in my code:

Option Explicit
Sub testme01()

Dim wks As Worksheet
Dim RngToCopy As Range
Dim DestCell As Range
Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim LastRow as long

Set wks = Worksheets("sheet1")

With wks
lastrow = .cells(.rows.count,"A").end(xlup).row
FirstCol = 2
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

For iCol = FirstCol To LastCol
Set RngToCopy = .Range(.Cells(1, iCol), _
.Cells(lastrow, iCol))
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)

DestCell.Resize(RngToCopy.Rows.Count, 1).Value _
= RngToCopy.Value
Next iCol

.Range(.Columns(FirstCol), .Columns(LastCol)).Delete

End With

End Sub

Change this line to what you need:
lastrow = .cells(.rows.count,"A").end(xlup).row
or even just plop in that value:
lastrow = 999



wrote:

Thankyou
The procedure for combining the columns worked as you said. I might
also have blank cells within the range that is combined into the single
column. I need to maintain the value of those cells (empty) when the
columns are combined into one. How do you account for empty cells and
keep them as empty in the single column?
Mary


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default combine columns

This works fine. What if lets say the 2nd column does not have
anything in the last row. I want for the routine to leave that cell
empty when it goes to the 1st column.
example:

a 1 10
b 2 12
c 3 aa
d 4 bb
e cc

should return
a
b
c
d
e
1
2
3
4

10
12
aa
bb
cc

Thanks for your help
Mary

Dave Peterson wrote:
That code copies each column from row 1 to the last used cell in that column.

Can you specify a column that defines the last used row?

I used column A in my code:

Option Explicit
Sub testme01()

Dim wks As Worksheet
Dim RngToCopy As Range
Dim DestCell As Range
Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim LastRow as long

Set wks = Worksheets("sheet1")

With wks
lastrow = .cells(.rows.count,"A").end(xlup).row
FirstCol = 2
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

For iCol = FirstCol To LastCol
Set RngToCopy = .Range(.Cells(1, iCol), _
.Cells(lastrow, iCol))
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)

DestCell.Resize(RngToCopy.Rows.Count, 1).Value _
= RngToCopy.Value
Next iCol

.Range(.Columns(FirstCol), .Columns(LastCol)).Delete

End With

End Sub

Change this line to what you need:
lastrow = .cells(.rows.count,"A").end(xlup).row
or even just plop in that value:
lastrow = 999



wrote:

Thankyou
The procedure for combining the columns worked as you said. I might
also have blank cells within the range that is combined into the single
column. I need to maintain the value of those cells (empty) when the
columns are combined into one. How do you account for empty cells and
keep them as empty in the single column?
Mary


--

Dave Peterson


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default combine columns

Change this line:
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
to
Set DestCell = destcell.Offset(rngtocopy.rows.count)



wrote:

This works fine. What if lets say the 2nd column does not have
anything in the last row. I want for the routine to leave that cell
empty when it goes to the 1st column.
example:

a 1 10
b 2 12
c 3 aa
d 4 bb
e cc

should return
a
b
c
d
e
1
2
3
4

10
12
aa
bb
cc

Thanks for your help
Mary

Dave Peterson wrote:
That code copies each column from row 1 to the last used cell in that column.

Can you specify a column that defines the last used row?

I used column A in my code:

Option Explicit
Sub testme01()

Dim wks As Worksheet
Dim RngToCopy As Range
Dim DestCell As Range
Dim iCol As Long
Dim FirstCol As Long
Dim LastCol As Long
Dim LastRow as long

Set wks = Worksheets("sheet1")

With wks
lastrow = .cells(.rows.count,"A").end(xlup).row
FirstCol = 2
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

For iCol = FirstCol To LastCol
Set RngToCopy = .Range(.Cells(1, iCol), _
.Cells(lastrow, iCol))
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)

DestCell.Resize(RngToCopy.Rows.Count, 1).Value _
= RngToCopy.Value
Next iCol

.Range(.Columns(FirstCol), .Columns(LastCol)).Delete

End With

End Sub

Change this line to what you need:
lastrow = .cells(.rows.count,"A").end(xlup).row
or even just plop in that value:
lastrow = 999



wrote:

Thankyou
The procedure for combining the columns worked as you said. I might
also have blank cells within the range that is combined into the single
column. I need to maintain the value of those cells (empty) when the
columns are combined into one. How do you account for empty cells and
keep them as empty in the single column?
Mary


--

Dave Peterson


--

Dave Peterson
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
Combine multiple columns into two long columns, Repeating rows in first column [email protected] Excel Discussion (Misc queries) 2 July 31st 06 09:45 PM
Combine multiple columns into two long columns, Repeating rows in first column [email protected] Excel Discussion (Misc queries) 0 July 31st 06 05:07 PM
How do you combine two columns to one? StTrumpet Excel Discussion (Misc queries) 4 March 22nd 05 02:10 AM
combine columns RDB Excel Worksheet Functions 3 March 15th 05 04:07 PM
Need to combine first and last name from two columns Dave[_38_] Excel Programming 2 January 10th 04 10:37 PM


All times are GMT +1. The time now is 07:24 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"