• Home
  • Blog
  • Resume
  • Contact
  • Projects
  • Gallery
  • Amit’s Resume
  • About Nagpur
KEEP IN TOUCH

Stack Columns of Data on one column

Dec23
2005
19 Comments Written by admin

I have modified this code for better explanation and error handling (includes a function to check if a worksheet exists or not). To run this code follow these steps:
1. Insert a new module in your workbook,
2. Copy and paste this code,
3. Go back to worksheet with data in it,
4. Press Alt + F8 to bring the macro window
5. Select this procedure and hit run
6. Enter the new worksheet name in the input box
7. If everything went well, you should have a new worksheet with all the data from original worksheet in one column with the column headers. See the screen shots for example.
Before stacking-the original data:
Before Stack
After stacking:
After Stack
Here’s the code:

Option Explicit
 
Sub Stack_cols()
 
On Error GoTo Stack_cols_Error
 
Dim lNoofRows As Long, lNoofCols As Long
Dim lLoopCounter As Long, lCountRows As Long
Dim sNewShtName As String
Dim shtOrg As Worksheet, shtNew As Worksheet
 
'Turn off the screen update to make macro run faster
Application.ScreenUpdating = False
'Ask for a new sheet name, if not provided use newsht
sNewShtName = InputBox("Enter the new worksheet name", "Enter name", "newsht")
'Set a sheet variable for the sheet where the data resides
Set shtOrg = ActiveSheet
'Add a new worksheet, rename it and set it to a variable
If Not SheetExists(sNewShtName) Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sNewShtName
Set shtNew = Worksheets(sNewShtName)
Else
MsgBox "Worksheet name exists. Try again", vbInformation, "Sheet Exists"
Exit Sub
End If
 
With shtOrg
'Get the last column number
'Replace .Range("IV1") with .Range("XFD1") for Excel 2007
lNoofCols = .Range("IV1").End(xlToLeft).Column
'Start a loop to copy and paste data from the first column to the last column
For lLoopCounter = 1 To lNoofCols
'Count the number of rows in the looping column
'Replace .Cells(65536, lLoopCounter) with .Cells(1048576, lLoopCounter) for Excel 2007
lNoofRows = .Cells(65536, lLoopCounter).End(xlUp).Row
.Range(.Cells(1, lLoopCounter), .Cells(lNoofRows, lLoopCounter)).Copy Destination:=shtNew.Range(shtNew.Cells(lCountRows + 1, 1), shtNew.Cells(lCountRows + lNoofRows, 1))
'count the number of rows in the new worksheet
lCountRows = lCountRows + lNoofRows
Next lLoopCounter
End With
 
On Error GoTo 0
SmoothExit_Stack_cols:
Application.ScreenUpdating = True
Exit Sub
 
Stack_cols_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub:Stack_cols"
Resume SmoothExit_Stack_cols
End Sub
'Check if a worksheet exists or not
Public Function SheetExists(sShtName As String) As Boolean
On Error Resume Next
 
Dim wsSheet As Worksheet, bResult As Boolean
bResult = False
Set wsSheet = Sheets(sShtName)
 
On Error GoTo 0
If Not wsSheet Is Nothing Then
bResult = True
End If
SheetExists = bResult
End Function

Related posts:

  1. Flip row or column
  2. Find the last filled column
  3. Find last filled column
  4. Save HTML files from one folder to Excel files in another folder
  5. Isworksheetopen and Isworkbookopen functions
Posted in Useful Procedures - Tagged excel, stack columns, VBA
SHARE THIS Twitter Facebook Delicious StumbleUpon E-mail
« Convert a number to its equivalent letter
» Random Numbers

19 Comments

  1. Anonymous's Gravatar Anonymous
    June 8, 2007 at 4:10 pm | Permalink

    did the trick, thanks you genius! JAmes UK)

  2. EfficienC's Gravatar EfficienC
    August 27, 2007 at 6:09 pm | Permalink

    could you perhaps show me how to implement your code? I’m having trouble getting results when running it. Thanks!

  3. Ashutosh's Gravatar Ashutosh
    August 29, 2007 at 12:47 pm | Permalink

    EfficienC:
    I’ve modified the code. I hope that this offers more explanation.

  4. Anonymous's Gravatar Anonymous
    September 27, 2007 at 3:55 am | Permalink

    Works PERFECTLY!!!! This just save me HOURS and HOURS of manual work. Thank you! – dub3

  5. Edward's Gravatar Edward
    February 6, 2008 at 8:42 am | Permalink

    Any chance this can be translated for Excel 2007? This seems like exactly the thing to save tons of time in excel.

    Thanks

    Edward

  6. Ashutosh's Gravatar Ashutosh
    February 6, 2008 at 1:22 pm | Permalink

    Edward,

    I’ve modified the code, and put two comments:
    ‘Replace .Range(“IV1″) with .Range(“XFD1″) for Excel 2007
    lNoofCols = .Range(“IV1″).End(xlToLeft).Column

    ‘Replace .Cells(65536, lLoopCounter) with .Cells(1048576, lLoopCounter) for Excel 2007
    lNoofRows = .Cells(65536, lLoopCounter).End(xlUp).Row

    You have to make two changes to make this work for 2007. First, change the last column from IV to XFD. Second, change the number of rows from 65536 to 1048576.

  7. Anonymous's Gravatar Anonymous
    May 1, 2008 at 4:29 pm | Permalink

    Much Appreciated!

  8. Anonymous's Gravatar Anonymous
    June 18, 2008 at 2:53 pm | Permalink

    In order for this to be even more effective, you could stack into two columns, with the first column repeating the header information for each stacked column. E.g.
    A 1
    A 2
    A 3
    B 1
    B 2
    B 3

  9. Tina's Gravatar Tina
    July 7, 2008 at 6:20 pm | Permalink

    Thanks!
    Does anyone know how to unstack a column? Say I have similar categories of information for 9 groups of 11 cells, all stacked in one column. I am looking to create a table (9×11)… I would very much appreciate your insights!

  10. Anonymous's Gravatar Anonymous
    August 31, 2008 at 10:30 pm | Permalink

    thankyou so much…saved me a whole lot of work :)

  11. dmiech's Gravatar dmiech
    November 18, 2008 at 2:36 pm | Permalink

    This is close to what I am looking for. How can I stack groups of columns? Like G groups of C columns? Or 10 Groups of 3 columns stacked in one group of 3 columns? It needs to be variable, because sometimes it might be 25 groups of 4 columns, etc. I can send an example of the data I have and how I need it to be arrange.

    Dan
    dmiech@aol.com

  12. darhay's Gravatar darhay
    July 10, 2009 at 9:56 am | Permalink

    EXCELLENT. WORKED VERY WELL.
    (took me a while, i’m not used to Macros, but I realized that when you copy this code, the ” and the ‘ don’t copy correctly. So make sure you replace those)

  13. a7n9's Gravatar a7n9
    July 10, 2009 at 10:22 am | Permalink

    @darhay,
    I’m glad that it worked for you, darhay. I’m sorry that the quotes come out wrong. Those single quotes got messed up when I transferred my posts from blogger to wordpress. I will see if I can replace them correctly.

  14. jbl's Gravatar jbl
    August 18, 2009 at 3:12 pm | Permalink

    that was so easy!!!!
    thank you so much for putting this on the web.

  15. andrew's Gravatar andrew
    October 19, 2009 at 6:05 am | Permalink

    Absolutely brilliant.

  16. Dink's Gravatar Dink
    July 15, 2010 at 10:28 am | Permalink

    Thanks….saved me loads of time.

  17. Iuliana's Gravatar Iuliana
    March 2, 2011 at 3:52 pm | Permalink

    It worked!!! That is so useful. Thank you

  18. Lisa D's Gravatar Lisa D
    May 18, 2011 at 2:46 pm | Permalink

    Thanks, you saved me tons of work!

  19. Amer's Gravatar Amer
    July 5, 2011 at 2:28 am | Permalink

    You just saved me days of work. Thank you

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Tags

Access Alt F8 Books boxplot cells charts count cursor dashboard data mining dbase design error excel excel functions export filter flip LaTex MS query Number Err ODBC pipes Press Alt F11 Public Sub python R random numbers Range Cells report scripting software sparklines SQL SQL server stack columns statistics stemming string tag cloud text mining UDF VBA visualization wildcard

Network

View Ashutosh Nandeshwar's profile on LinkedIn

Recent Comments

  • W. McNabb on The search key was not found in any record in Access
  • Manuel on The search key was not found in any record in Access
  • Wendy Naples on The search key was not found in any record in Access
  • larry on Access Export to Excel (2007)
  • Betty Chou on Projects

Related Posts

  1. Flip row or column
  2. Find the last filled column
  3. Find last filled column
  4. Save HTML files from one folder to Excel files in another folder
  5. Isworksheetopen and Isworkbookopen functions

EvoLve theme by Blogatize  •  Powered by WordPress nandeshwar.info