+ Reply to Thread
Results 1 to 10 of 10

Macro for turning "titles" into values of new columns (transposing and repeating values)

  1. #1
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Question Macro for turning "titles" into values of new columns (transposing and repeating values)

    Hi everyone,
    I would badly need a macro to go from what you see on the "what_I_have" sheet to the "what_I_need" sheet in the attached file.
    Basically, in this badly formatted file from which I need to start from, there are three "titles" for each column and I need to move two of these "titles" to the values of two new columns I need to create, so that in the end there is just one type of values in each column.

    This is the beginning of the table:

    Year year_1 year_1
    Month January January
    MeasuresLevel Variable1 Variable2
    type_1 continent_1 country_1 0 0
    type_1 continent_2 country_2 0 0
    type_2 continent_1 country_3 2 2
    type_2 continent_2 country_4 0 0
    type_3 continent_1 country_5 20 74
    type_3 continent_2 country_6 22 123

    As you can see "Year" and "Month" are like titles and I need to convert them to row values like this:

    Year Month Type Continent Country Variable1 Variable2
    year_1 January type_1 continent_1 country_1 0 0
    year_1 January type_1 continent_2 country_2 0 0
    year_1 January type_2 continent_1 country_3 2 2
    year_1 January type_2 continent_2 country_4 0 0
    year_1 January type_3 continent_1 country_5 20 74
    year_1 January type_3 continent_2 country_6 22 123

    Moreover, the starting file contains many years (and is therefore much longer than the example file I attach, which contains only 1 year).

    Could anyone come up with a macro that does this?
    I would be extremely grateful, doing this by hand is impossible because I have a lot of files and a lot of years of data.
    Thanks a lot!



    example.xlsx
    Last edited by Wilco84; 02-23-2015 at 05:38 PM.

  2. #2
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    I'm sorry if this task is too difficult...
    Could anyone point me to some partial solution maybe?
    Thanks!

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Hi Wilco,

    Are any of your cells "merged cells". If they are this problem gets a lot harder. Can you attach a workbook sample instead of a picture? To attach a workbook, click on "Go Advanced" and then on the Paper Clip Icon above the advanced message area. I want to write code to do this type of problem and your example would help.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Dear Marvin,
    I already uploaded the example file, you can download it by simply clicking on "example.xlsx" at the end of my first post.
    I attach it again here: example.xlsx
    I don't know what you mean by merged cells, but this file is a clean output of a database software on which I don't have any control, so I have to work starting from this.
    For the moment, I have written a macro to copy and paste "down" just the values:

    Please Login or Register  to view this content.
    Now I would need to "copy down" the first rows of the first three columns and then add two additional columns on the left that repeat the value of the year and of the month in the correct positions.

    Thanks a lot for your time!

  5. #5
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Just an update on my progress: I managed to "copy down" the first rows of the first three columns, so now I "only" need to figure out how to create two additional columns and populate them with the correct values of Year and Month.
    My code so far:

    Please Login or Register  to view this content.
    Thanks

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Hi Wilco84,

    I'm working on this problem still. I'm trying to make it work for any number of fixed columns to the left of the data and any number of fixed rows above the data. I should be done by tomorrow (or later tonight if I get lucky).

  7. #7
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Thank Marvin,
    here's my solution, probably highly inefficient (I just learned VBA just to do this):

    Please Login or Register  to view this content.
    I would also need to add two more columns to each sheet depending on the sheet name, but I guess I'll do that by hand.
    Please post your generalized solution anyway, I think it could be helpful for other people.
    Cheers!

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Hey Wilco,

    This problem has been on my list for a while now. I've written other code to turn Crosstab Tables in to Excel tables but never with your type of Row Headings. This is the first run on this guy and should work for your specific data. To generalize this problem is much harder than my other code. I think you know why.

    I ask 2 questions to try to generalize this code to work with many different arrangements of Crosstab Tables with different number of fixed rows or columns.

    1. How many Columns of data are fixed. This is the number of Columns on the left, that need to be repeated.
    2. How many Rows of data (at the top) are fixed. This is the number of Rows, at the top, that need to be repeated.

    I didn't do this exactly like you had, as you had two columns of values. I did this macro to have only one value column. Pivot tables and normal tables will work with this better.

    Give this a try and let me know how to improve it. Click the Star below this post to give me rep if it works as I always look at those replies first.
    Last edited by MarvinP; 02-24-2015 at 01:37 AM.

  9. #9
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Thanks Marvin,
    I solved my problem with the code above so I won't try it right now, but when I do I'll let you know if it works fine with my data.
    It would be nice to also allow for different numbers of value columns, in my opinion.
    Thanks again.
    Cheers!

  10. #10
    Registered User
    Join Date
    07-15-2013
    Location
    Italia
    MS-Off Ver
    Excel 2011
    Posts
    17

    Re: Macro for turning "titles" into values of new columns (transposing and repeating value

    Dear Marvin,
    I tried your macro and it works very well, thank you!
    There are just a couple of things that could be further improved, but at very little cost: the resulting sheet has two blank rows on top and it would be very nice to be able to assign the column names at the beginning, asking the user what the fixed rows and columns represent. The result of your effort is very similar to the "melt" function result of the Reshape package in R.
    Thanks again!
    I'd give you another star but it tells me to give it to someone else before giving it to you again!
    Cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 01-21-2015, 09:46 PM
  2. Replies: 0
    Last Post: 11-22-2013, 01:36 PM
  3. Replies: 0
    Last Post: 01-09-2013, 06:52 PM
  4. Replies: 3
    Last Post: 02-16-2011, 02:55 PM
  5. sum values in "B" if repeating in "A"
    By uncontious in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-09-2007, 10:15 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1