1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Excel multiple columns to one

Discussion in 'Business Applications' started by StoryAngel, Aug 15, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. StoryAngel

    StoryAngel Thread Starter

    Joined:
    May 29, 2002
    Messages:
    97
    I have a huge spreadsheet which I am trying to analyse but I really need some help! There is one row of data for each company, then separate columns for products. I need one single column so I can use functions like COUNTIF. A simplified example:

    Current situation
    Company 1 | Product a | Product d | Product f
    Company 2 | Product b
    Company 3 | Product a | Product f

    Desired result

    Company 1 | Product a
    Company 1 | Product d
    Company 1 | Product f
    Company 2 | Product b
    Company 3 | Product a
    Company 3 | Product f

    I am SURE there must be a way of doing this, but I am going round and round in circles right now.... thanks for helping! :)
     
  2. Jimmy the Hand

    Jimmy the Hand

    Joined:
    Jul 28, 2006
    Messages:
    1,223
    Hi!

    Here is a small macro, which does what you need. (See attachment.)
    Open sorter.xls
    Copy your big spreadsheet to Sheet1
    Start macro called "sorter"
    It creates a new sheet with the name "Destination", and creates a list of your data in the desired way. At least, it works with your sample.

    cheers
     

    Attached Files:

  3. StoryAngel

    StoryAngel Thread Starter

    Joined:
    May 29, 2002
    Messages:
    97
    Thanks a lot! The original data isn't quite so clean as my example (and is nearly 2000 rows), but hopefully I can tweak that macro and make it behave as it should. Thanks again :)
     
  4. Zack Barresse

    Zack Barresse

    Joined:
    Jul 25, 2004
    Messages:
    5,452
    I always opt for a For/Next instead of a Do/Loop. Maybe it's just me, I'd use ..

    Code:
    Sub Sorter2()
        Dim ws As Worksheet, wsNew As Worksheet
        Dim i As Long, j As Long, r As Long
        Set ws = Sheets("Sheet1")
        Set wsNew = Worksheets.Add
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        On Error Resume Next
        Worksheets("Destination").Delete
        wsNew.Name = "Destination"
        r = 2
        For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
            For j = 2 To ws.Cells(i, ws.Columns.Count).End(xlToLeft).Column
                wsNew.Cells(r, 1).Value = ws.Cells(i, 1).Value
                wsNew.Cells(r, 2).Value = ws.Cells(i, j).Value
                r = r + 1
            Next j
        Next i
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
    End Sub
    Another option I guess.
     
  5. StoryAngel

    StoryAngel Thread Starter

    Joined:
    May 29, 2002
    Messages:
    97
    Firefytr/Zack, your answer looks very impressive but I need to have it translated into laymans' terms! Do I need to use that as a macro, and if not how do I implement what you typed? I'm definitely open to other ideas. Thanks!
     
  6. Zack Barresse

    Zack Barresse

    Joined:
    Jul 25, 2004
    Messages:
    5,452
    Hi there,

    Yes, it's a macro/procedure (macro means something else in Access). To use it, have the file in question open.

    Press Alt + F11 to open the Visual Basic Editor (VBE)
    Press Ctrl + R to open the Project Explorer (PE)
    Select your file/project in bold on left (PE pane)
    Click Insert (menu), Module
    Paste the code I posted in the right [module] window/pane
    Close the VBE
    Press Alt + F8 (or Tools | Macro | Macros..)
    Select the macro/procedure (Sorter2)
    Click Run.

    NB: Whatever sheet your data is on, change this line to match the sheet name ...

    Code:
        Set ws = Sheets("Sheet1")
    This assumes the sheet name is Sheet1. I prefer to use sheet names, positions or code names rather than activesheet. Too easy to screw up that way. This routine will create a new sheet with your data, leaving your original data intact.

    HTH
     
  7. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/492613

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice