+ Reply to Thread
Results 1 to 10 of 10

Duplicate line and transpose data

  1. #1
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Duplicate line and transpose data

    If I do this manually, it’s easy to identify because I can see if the worksheet actually has the 4 quarterly columns in it. But the process is tedious when there are many data rows. If there are 100 lines, I need to repeat the same process 100 times. I want to build a macro to break each line with 4 quarterly score into four separate lines. The macro can change the data directly on the same worksheet, no need to create new worksheet.

    Here are the steps.
    1. Find 4 quarterly average, however, the heading texts are not consistent. These are the heading I can identify so far: Qtr1, Q01, Q’01, Q1’2014 , Q1’14.
    2. Note: To make it even more difficult, there may be other column in between “Teacher”, “School”, “Class Number”, “Location”, “Average”, but the Q1 to Q4 column will almost always be continuous sequence (no other column in between them).
    3. If the macro can’t find the 4 columns (qtr1 to qty4) with average data, then exit sub.
    4. If found, do this.
      • Insert a new column, after “Location”, call it “C1”.
      • Insert a new column, call it “Qtr Avg”, at the last column .
      • Go to first teacher (first data row), copy the line.
      • Duplicate it 3 times, insert the lines below the line it copied.
      • In “C1” column, get the value from column “Class Number”, then add “-R-Q0#”. # is the quarterly number from 1 to 4.
      • In “Qtr Avg” column for each of the 4 lines, get/copy the respective quarter average. Then round up the value with no decimal point e.g. round(average, 0).
      • Go to next teacher. Repeat steps C to G.
      • Once the last data row is done, we can remove these 4 columns “Average”, “Q1”, “Q2”, “Q3” and “Q4”.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Duplicate line and transpose data

    hi Efjoker, option, please check attachment, press Run button or runcode "test" (ALT+F8, select "test", press Run button).

    You asked not to put result to a separate sheet but for testing it was done on a separate sheet for now. If you are happy with the testing results I will amend it to be on the same sheet.
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,947

    Re: Duplicate line and transpose data

    While you can use this macro, which will speed things up a lot, your manual technique could be simplified to a 1 time procedure rather than the 100 time procedure you are currently using.

    When you need to 'fold data', the best way is to do blocks at a time, and use formulas to move the data. For example, this is how to process your table:

    First, copy all your data (except the headers), and paste it below the existing data 3 times, so that you end up with each line repeated four times. Then sort your data based on column A. This will move the data together.

    Then, insert two new columns, and label the first "C1" and the second "Qty Avg" and in rows 2 to 5 of C1, use these four formulas:

    =D2 & "--Q1"
    =D3 & "--Q2"
    =D4 & "--Q3"
    =D5 & "--Q4"

    Where D is the column with the Class Number.

    Then in "Qty Avg" and use these four formulas

    =L2
    =M3
    =N4
    =O5

    Where L, M, N, and O are the quarter values.

    Then copy those eight formulas to fill the two columns below. Copy both columns and pastespecial values, and delete the Average and quarter value columns. And you're done.

    Anyway, here is the macro.


    Please Login or Register  to view this content.
    Bernie Deitrick
    Excel MVP 2000-2010

  4. #4
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Duplicate line and transpose data

    Hi waterserv,
    Thanks for helping. Your macro works very well.

    Yes I want to put the result on the same worksheet. My idea is to save your code in a seperate xls file (e.g. Me.xls) in a module. It's like a repository of macros that I use daily. So this way, I don't have to repaste the macro in 4Q file when someone else updates it.

    I open this Me.xls. Run your macro. The macro will launch a Open file dialogbox. I select the 4Q.xls file. It will look for the worksheet called "Original-Work", your "test" macro continue to update/change the data on the same "Original-Work" worksheet.

    Thank you.
    Last edited by Efjoker; 09-18-2014 at 03:30 AM.

  5. #5
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Duplicate line and transpose data

    Hi Bernie, I tried your macro too. Yes it got the job done. Thank you very much.

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Duplicate line and transpose data

    please check attachment
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Duplicate line and transpose data

    After I try the macro on more files, I found one exception.

    There’s rare case where the line only has an average value, but none quarterly value. When I see this, I don't have to duplicate the line. The C1 column will have “Class Number” + “-R-A01”. This “-R-A01” is a fixed string. Then the Qty Avg will get the value from Average column. The file will look like 4Qa.xls.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Duplicate line and transpose data

    please check attachment
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Duplicate line and transpose data

    I see. I tried similar if-then statement last night like you what did in 4Q(2), but I didn't know how to exit the loop to stop the repetition. The "exit for" is needed. Thank you very much.

  10. #10
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Duplicate line and transpose data

    watersev, how can I remove the formatting, and assign only values to the result() array?
    I don't want to carry over the fill and font color, font type etc.


    Ans: I found .ClearFormats, problem solved.

    Please Login or Register  to view this content.
    Last edited by Efjoker; 09-19-2014 at 03:28 AM.

+ 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. [SOLVED] Transpose multiple duplicate rows into relevant column data
    By SimonN in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-27-2013, 06:51 AM
  2. [SOLVED] Transpose all but duplicate data
    By FranAgrippina in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-30-2013, 10:45 AM
  3. [SOLVED] Transpose data from a excel template in a line wise
    By itsmesunilb in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-02-2012, 12:59 PM
  4. Macro to Duplicate Line + Transpose Data
    By flarel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-28-2012, 05:07 AM
  5. How to collapse Data into one line (similar to a transpose, but not quite)
    By dyoung66 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-05-2008, 11:48 PM

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