LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Combine several columns of different length into one single co

Thank you very much for your help Norman, it is much appreciated!

And for anybody else who may be browsing the NG for advice on this matter,
Norman very kindly provided me with an updated code, which ensures that the
results are exactly the same either when the initial columns are headed by
blank cells, or when headed by cells containing data. It also ensures that
column K retains its original interior colour (please note that it now
functions on the active sheet):

'================
Public Sub Tester001A()
Dim SH As Worksheet
Dim rng As Range
Dim srcRng As Range
Dim destRng As Range
Dim col As Range
Dim LastRow As Long
Dim iColour As Long 'NEW VARIABLE

Set SH = ActiveSheet
Set rng = SH.Range("A:J")

With SH
iColour = .Cells(1, "K").Interior.ColorIndex ''NEW CODE LINE
.Columns("K:K").ClearContents
For Each col In rng.Columns
LastRow = .Cells(Rows.Count, col.Column).End(xlUp).Row
Set srcRng = col.Cells(1).Resize(LastRow)
Set destRng = .Cells(Rows.Count, "K").End(xlUp)(2)
srcRng.Copy Destination:=destRng
Next col

On Error Resume Next
Range("K:K").SpecialCells(xlBlanks).Delete Shift:=xlUp
On Error GoTo 0

'NEW CODE LINE
Intersect(.Range("K:K"), .UsedRange).Interior.ColorIndex = iColour

End With

End Sub
'<<================

I cannot stress enough how useful this code has been, thanks again Norman!




"Norman Jones" wrote:

Hi Neil,

Re-reading your post, I see that I have overlooked your requirement:

(ignoring blanks)


Therefore, please replace my suggested code with the following version:

'================
Public Sub Tester001()
Dim WB As Workbook
Dim SH As Worksheet
Dim rng As Range
Dim srcRng As Range
Dim destRng As Range
Dim col As Range
Dim LastRow As Long

Set WB = Workbooks("YourBook.xls") '<<===== CHANGE
Set SH = WB.Sheets("Sheet2") '<<===== CHANGE
Set rng = SH.Range("A:J")

With SH
.Columns("K:K").ClearContents
For Each col In rng.Columns
LastRow = .Cells(Rows.Count, col.Column).End(xlUp).Row
Set srcRng = col.Cells(1).Resize(LastRow)
Set destRng = IIf(IsEmpty(Range("K1")), .Range("K1"), _
.Cells(Rows.Count, "K").End(xlUp)(2))
destRng.Select
srcRng.Copy Destination:=destRng
Next col

On Error Resume Next
Range("K:K").SpecialCells(xlBlanks).Delete Shift:=xlUp
On Error GoTo 0

End With

End Sub
'<<================


---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Neil,

Taking the opportunity to correct a typo, try instead:

'================
Public Sub Tester001()
Dim WB As Workbook
Dim SH As Worksheet
Dim rng As Range
Dim srcRng As Range
Dim destRng As Range
Dim rcell As Range
Dim col As Range
Dim LastRow As Long

Set WB = Workbooks("YourBook.xls") '<<===== CHANGE
Set SH = WB.Sheets("Sheet2") '<<===== CHANGE
Set rng = SH.Range("A:J")

With SH
.Columns("K:K").ClearContents '<< ==== Typo corrected
For Each col In rng.Columns
LastRow = .Cells(Rows.Count, col.Column).End(xlUp).Row
Set srcRng = col.Cells(1).Resize(LastRow)
Set destRng = IIf(IsEmpty(Range("K1")), .Range("K1"), _
.Cells(Rows.Count, "K").End(xlUp)(2))
destRng.Select
srcRng.Copy Destination:=destRng
Next col
End With

End Sub
'<<================


---
Regards,
Norman




 
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 seperate rows into single column Will Excel Discussion (Misc queries) 1 February 11th 09 03:11 PM
How do I combine stacked column and single column graphs? Alf Excel Discussion (Misc queries) 0 February 24th 06 12:29 AM
How do you combine a stacked column and single column graph? Charlotte C Charts and Charting in Excel 1 December 3rd 05 01:04 PM
how to combine several columns into a single column jims Excel Discussion (Misc queries) 9 August 15th 05 12:00 PM
How can I combine IF, COLUMN, and LARGE formulas in a single cell? Liam Judd Excel Worksheet Functions 1 November 17th 04 07:52 AM


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