+ Reply to Thread
Results 1 to 15 of 15

Macro to Transpose Data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-04-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    161

    Macro to Transpose Data

    Looking for a macro that will transpose my data. See attached example.

    *As multiple items will be transposed. Date and Month are important, the actual day is not.


    Capture.JPG


    Thanks so much in advance.
    Attached Files Attached Files

  2. #2
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to Transpose Data

    Using your uploaded file, this seemed to work.

    Sub t()
    Dim c As Range, fn As Range, i As Long
        With Sheets("Sheet1")
            For Each c In .Range("G2", .Cells(Rows.Count, 7).End(xlUp))
                Set fn = Sheets("Result").Range("C2", Sheets("Result").Cells(Rows.Count, 3).End(xlUp)).Find(c.Offset(, -4).Value, , xlValues, xlWhole)
                    If Not fn Is Nothing Then
                        With Sheets("Result")
                            For i = 6 To .Cells(1, Columns.Count).End(xlToLeft).Column
                            'MsgBox Month(.Cells(1, i).Value) & vbLf & Month(c.Offset(, -1).Value)
                                If Month(.Cells(1, i).Value) = Month(c.Offset(, -1).Value) And _
                                Year(.Cells(1, i).Value) = Year(c.Offset(, -1).Value) Then
                                    .Cells(fn.Row, i) = c.Value
                                End If
                            Next
                        End With
                    End If
            Next
        End With
    End Sub
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  3. #3
    Forum Contributor
    Join Date
    10-04-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Macro to Transpose Data

    JLGWhiz - thanks for the quick reply.
    I messed up my example and am going to I will mark this as completed & repost the question.


    Moderator Comment: Do not duplicate a thread. Continue in the original thread with the changes. I have merged the two threads.
    Last edited by alansidman; 12-16-2020 at 12:29 AM.

  4. #4
    Forum Contributor
    Join Date
    10-04-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    161

    Transpose Data onto New Sheet

    Looking for a macro that will transpose my monthly data onto a new sheet.

    *As multiple items will be transposed. Date and Month are important, the actual day is not.

    Thanks so much in advance.



    Capture4.JPG
    Attached Files Attached Files

  5. #5
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Transpose Data onto New Sheet

    that's probably advanced. lots of work. post in the commercial services section perhaps? there is a built-in function called transpose, however it will not give you want you. only partially. maybe try it and see? see if it gives you something close to what you want, and then ask for the last little bit? that's an idea.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,412

    Re: Transpose Data onto New Sheet

    There is absolutely no need for the OP to pay for a solution in Commercial Services. This can be done easily with Power Query, and there will be formula solutions, too. It’s not a lot of work.

    I will share a PQ solution tomorrow if nobody else gets in first.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  7. #7
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Transpose Data onto New Sheet

    I had no idea. thanks for saying. you're the PQ expert, not me.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,412

    Re: Transpose Data onto New Sheet

    Quote Originally Posted by vba_php View Post
    I had no idea. thanks for saying. you're the PQ expert, not me.
    If you don't know whether something can be done or not, it's best not to respond to the thread, as I have advised you before. By doing so, you remove the thread from the unanswered threads list for no good reason: speculation doesn't get the OP any further forward. Please try not to do this. Thanks.

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Transpose Data onto New Sheet

    As Ali indicated. A fairly simple task for Power Query. Mcode:

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer Location.", Int64.Type}, {"Plant Desc.", type text}, {"Product", Int64.Type}, {"Prod.Desc.", type text}, {"Unit of Measure", type text}, {"Deliv. Date", type date}, {"Document Qty.", Int64.Type}}),
        #"Inserted Start of Month" = Table.AddColumn(#"Changed Type", "Start of Month", each Date.StartOfMonth([Deliv. Date]), type date),
        #"Removed Columns" = Table.RemoveColumns(#"Inserted Start of Month",{"Deliv. Date"}),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Start of Month", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Start of Month", type text}}, "en-US")[#"Start of Month"]), "Start of Month", "Document Qty.")
    in
        #"Pivoted Column"
    Excel 2016 (Windows) 32 bit
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    1
    Customer Location. Plant Desc. Product Prod.Desc. Unit of Measure 12/1/2020 1/1/2020 2/1/2020 6/1/2021 1/1/2021 2/1/2021
    2
    11111
    Customer - 1
    1075176
    Product Description KG
    50
    3
    11111
    Customer - 1
    10046903
    Product Description KG
    50
    534
    890
    4
    22222
    Customer - 2
    10045134
    Product Description KG
    174
    1234
    806
    5
    22222
    Customer - 2
    10049999
    Product Description KG
    25
    Sheet: Table1

    Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

    It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

    - Follow this link to learn how to install Power Query in Excel 2010 / 2013.

    - Follow this link for an introduction to Power Query functionality.

    - Follow this link for a video which demonstrates how to use Power Query code provided.
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro to Transpose Data

    hftechno

    Didn't you ask similar question?

    VBA
    Sub test()
        Dim a, x, i As Long, ii As Long, txt As String, AL As Object
        Set AL = CreateObject("System.Collections.ArrayList")
        a = Sheets("sheet1").Cells(1).CurrentRegion.Value
        ReDim b(1 To UBound(a, 1), 1 To 50)
        For ii = 1 To 5: b(1, ii) = a(1, ii): Next
        With CreateObject("Scripting.Dictionary")
            For i = 2 To UBound(a, 1)
                a(i, 6) = a(i, 6) - Day(a(i, 6)) + 1
                If Not AL.Contains(a(i, 6)) Then AL.Add a(i, 6)
                txt = Join(Array(a(i, 1), a(i, 2), a(i, 3), _
                            a(i, 4), a(i, 5)), Chr(2))
                If Not .exists(txt) Then
                    Set .Item(txt) = CreateObject("Scripting.Dictionary")
                End If
                .Item(txt)(a(i, 6)) = .Item(txt)(a(i, 6)) + a(i, 7)
            Next
            AL.Sort
            ReDim Preserve a(1 To UBound(a, 1), 1 To 5)
            ReDim Preserve a(1 To UBound(a, 1), 1 To AL.Count + 5)
            For ii = 0 To AL.Count - 1: a(1, ii + 6) = AL(ii): Next
            For i = 0 To .Count - 1
                x = Split(.keys()(i), Chr(2))
                For ii = 0 To UBound(x)
                    a(i + 2, ii + 1) = x(ii)
                Next
                For ii = 0 To .items()(i).Count - 1
                    a(i + 2, AL.IndexOf_3(.items()(i).keys()(ii)) + 6) = .items()(i).items()(ii)
                Next
            Next
            i = .Count + 1
        End With
        With Sheets.Add.Cells(1).Resize(i, AL.Count + 5)
            .Value = a
            .HorizontalAlignment = xlCenter
            .Columns("f").Resize(, AL.Count).NumberFormat = "#,##0.00"
            .Columns.AutoFit
        End With
    End Sub

  11. #11
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Macro to Transpose Data

    sure thing.

  12. #12
    Forum Contributor
    Join Date
    10-04-2014
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Macro to Transpose Data

    This is the best forum

    - AliGW you are hilarious! Thanks for keeping law and order.
    - alansidman , thanks so much for all of the detail. I have never used Power Query before ...but will definitely do so now. Your solution worked!
    - jindon, your answer worked as well, thanks as always.

    I chose jindon's because it is a ready made macro.

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,412

    Re: Macro to Transpose Data

    We aim to please.

  14. #14
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Macro to Transpose Data

    @hitechno - Thanks for the reps. The code I posted also works on the revised sheets.
    Regards, JLG

  15. #15
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Macro to Transpose Data

    Also, thank you for the reps.

+ 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] Need VBA Macro to transpose data
    By vbronton in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 12-03-2019, 09:17 AM
  2. Transpose data macro
    By makinmomb in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-11-2015, 05:16 PM
  3. [SOLVED] Transpose Data Using Macro
    By Jbrowning1980 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2015, 12:57 PM
  4. [SOLVED] macro to transpose data
    By rhoda20 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-08-2014, 08:39 AM
  5. [SOLVED] Macro To Transpose Data
    By AlexRoberts in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2014, 09:33 AM
  6. Help with macro to transpose data
    By ejecheche in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-03-2012, 06:26 AM
  7. Transpose Data Macro
    By Jakila2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2011, 03:56 PM

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