+ Reply to Thread
Results 1 to 4 of 4

Best Way to Transform Data into Desired Format

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2013
    Location
    Beirut
    MS-Off Ver
    Excel OSX
    Posts
    38

    Post Best Way to Transform Data into Desired Format

    Quite frequently I have to transform data into the the format found in tab ''Original'' to the format in tab ''Desired''.

    I was wondering what the quickest and most efficient way to do this would be: Pivot Tables? Formulas? I would typically use lookup formulas to match the country name to the year, but that is time consuming since in order to do that I would have to set up and format the sheet beforehand.

    Any suggestions are more than welcome! Thanks.
    Attached Files Attached Files

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Best Way to Transform Data into Desired Format

    Try this macro

    Sub Test()
    Sheets("Original").Activate
    Sheets("Desired").Cells.Clear
    Range("A1:F1").Copy Destination:=Sheets("Desired").Range("A1:F1")
    TargetRow = 1
    For N = 2 To Cells(Rows.Count, 1).End(xlUp).Row
        If Cells(N, 1) <> Cells(N - 1, 1) Or Cells(N, 2) <> Cells(N - 1, 2) Or Cells(N, 3) <> Cells(N - 1, 3) Or Cells(N, 4) <> Cells(N - 1, 4) Or Cells(N, 5) <> Cells(N - 1, 5) Or Cells(N, 6) <> Cells(N - 1, 6) Then
            TargetRow = TargetRow + 1
            Range(Cells(N, 1), Cells(N, 6)).Copy Destination:=Sheets("Desired").Cells(TargetRow, 1)
        End If
        If WorksheetFunction.CountIf(Sheets("Desired").Rows(1), Cells(N, 7)) = 0 Then
            Sheets("Desired").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = Cells(N, 7)
        End If
        TargetColumn = Sheets("Desired").Rows(1).Find(Cells(N, 7), , xlValues, xlWhole).Column
        Sheets("Desired").Cells(TargetRow, TargetColumn) = Cells(N, 8)
    Next N
    End Sub

    Open up the VBA editor by hitting ALT F11

    Insert a new module by hitting Insert - Module

    Paste the macro into the empty sheet

    Hit ALT F11 to get back to the worksheet.

    Run the macro by going to tools-macro in Excel 2003 or the view ribbon in Excel 2007/2010.
    Martin

  3. #3
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Best Way to Transform Data into Desired Format

    @AnitPivotTable

    Still with an pivot table.

    See the attached file.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Best Way to Transform Data into Desired Format

    And yet another:

    Sub APT(): Dim i As Long, j As Long, k As Long, wo As Worksheet, wd As Worksheet
    Set wo = Sheets("Original"): Set wd = Sheets("Desired"): j = 2: k = 2
    GetHeads: wo.Rows(1).copy wd.Range("A1")
    i = 2: Do Until wo.Cells(i + 1, 7) < wo.Cells(i, 7): i = i + 1: Loop
    wo.Cells(2, 7).Resize(i - 1, 1).copy: wd.Cells(1, 7).PasteSpecial Transpose:=True
    LoadDetail: Do Until wo.Cells(j, 1) = ""
    wd.Cells(k, 1).Resize(1, 6).Value = wo.Cells(j, 1).Resize(1, 6).Value
    wo.Cells(j, 8).Resize(i - 1, 1).copy: wd.Cells(k, 7).PasteSpecial Transpose:=True
                j = j + i - 1: k = k + 1: Loop: End Sub
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

+ 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] Macro to format raw data in to desired formatted data file.
    By bmbalamurali in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 03-21-2014, 01:49 PM
  2. VBA to Transform and format data
    By rggovani in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-14-2013, 09:18 AM
  3. transform text to a DD:HH:MM:SS format
    By akasha303 in forum Excel General
    Replies: 4
    Last Post: 10-19-2011, 09:31 AM
  4. Transform the format of data
    By caldera55 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-17-2008, 01:20 PM
  5. [SOLVED] Transform text in date format
    By ina in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2006, 03:35 AM

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