+ Reply to Thread
Results 1 to 5 of 5

Any Easy VBA to do the transpose?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-01-2016
    Location
    Toronto, Canada
    MS-Off Ver
    2010
    Posts
    3

    Any Easy VBA to do the transpose?

    Hello, I am learning VBA from Scratch and the codes are flying over my head.


    I was wondering if there is any easy to understand codes rather than complex ones so that I can understand and learn it forever. I have attached the File here.


    I am trying to transpose the attached file's (file has many rows/ coulmns, so was hoping someone will actually take a look at it) "Data" tab's Column C to BV and put them in one column in the transposed_Data" tab by "item" and by "product".


    From the raw data to this:

    Labor Cost Product_1 1/1/2014
    Labor Cost Product_1 2/1/2014
    Labor Cost Product_1 3/1/2014
    Labor Cost Product_1 4/1/2014
    Labor Cost Product_1 5/1/2014
    Labor Cost Product_1 6/1/2014
    Labor Cost Product_1 7/1/2014

    Labor Cost Product_2 1/1/2014
    Labor Cost Product_2 2/1/2014
    Labor Cost Product_2 3/1/2014
    Labor Cost Product_2 4/1/2014
    Labor Cost Product_2 5/1/2014
    Labor Cost Product_2 6/1/2014
    Labor Cost Product_2 7/1/2014

    and so on for all "items" and for all "Products"
    Attached Files Attached Files

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: Any Easy VBA to do the transpose?

    I might suggest that this is not the best spreadsheet to learn VBA on. The data are poorly organized. What is eye-appealing for humans is often very complicated for the machine.

    In this case the dates are strung out over the columns. The way the computer would like the data is:
    Items | Product | Date | Costs
    and in fact, that's what I'd do if I were programming this: read what you have and reformat it to look like this and then a pivot table could take care of just about any way you want to present the data.

    To program this, you would have to know how to address pages, loop through ranges and populate a table. If you feel comfortable with this, we can give it a try.
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Any Easy VBA to do the transpose?

    Quote Originally Posted by dflak View Post
    I might suggest that this is not the best spreadsheet to learn VBA on. The data are poorly organized. What is eye-appealing for humans is often very complicated for the machine.
    Isnt this what theyre trying to sort out though? or have I picked the OP up wrong?

    The code below will transpose the data into what you want, Ive commented the code where possible to explain what it does.

    Sub Transposer()
    Dim xRow, xCol, LastRow, LastCol, outputrow As Double
    
    Dim sht, outsht As Worksheet
    Dim strItem, strProd, strDate As String
    
    'setup the range to be analysed
    Set sht = Worksheets("Data")
    Set outsht = Worksheets("Transposed_Data")
    LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row 'works out how many rows there are in the Data sheet
    LastCol = sht.Cells(3, sht.Columns.Count).End(xlToLeft).Column 'and the same for the number of columns
    
    outputrow = 1  'set up where the output data in Transposed_Data starts from.
    For xRow = 4 To LastRow  'run through a loop of all the rows in the data sheet.
    strItem = Cells(xRow, 1).Value 
    strProd = Cells(xRow, 2).Value
    
        For xCol = 3 To LastCol  'loop through all the columns on that row
        If (Val(Cells(xRow, xCol).Value) > 0) Then  'if theres a value in the cell then we want to output the row to the Transposed_Data sheet.
            strDate = Cells(3, xCol).Value
            
            'output the values into the first 4 columns       
            outsht.Cells(outputrow, 1).Value = strItem
            outsht.Cells(outputrow, 2).Value = strProd
            outsht.Cells(outputrow, 3).Value = strDate
            outsht.Cells(outputrow, 4).Value = sht.Cells(xRow, xCol).Value
            outputrow = outputrow + 1   'move to the next row of the output sheet
        End If
        Next xCol   'go to the next column
    Next xRow     'go to the next row
    End Sub
    Last edited by pjwhitfield; 11-01-2016 at 05:29 PM.
    If someone has helped you then please add to their Reputation

  4. #4
    Registered User
    Join Date
    11-01-2016
    Location
    Toronto, Canada
    MS-Off Ver
    2010
    Posts
    3

    Re: Any Easy VBA to do the transpose?

    Quote Originally Posted by pjwhitfield View Post
    Isnt this what theyre trying to sort out though? or have I picked the OP up wrong?

    The code below will transpose the data into what you want, Ive commented the code where possible to explain what it does.

    Hi pjwhitfield

    Yes, you are correct, I am actually trying to sort out the data dump. Many thanks for understanding.

    And also, Many thanks for the code, looks simple. I am going to implement it in the Real File and will come back if there is any issue or something i do not understand.

  5. #5
    Registered User
    Join Date
    11-01-2016
    Location
    Toronto, Canada
    MS-Off Ver
    2010
    Posts
    3

    Re: Any Easy VBA to do the transpose?

    Quote Originally Posted by pjwhitfield View Post

    The code below will transpose the data into what you want, Ive commented the code where possible to explain what it does.
    Hi pjwhitfield

    It is working!! many thanks, you are awesome!! One thing I was wondering about, is it possible to show the blanks as 0 or jsut show the blanks in the output tab ("transposed_data")? currently, it is simply omitting the blanks and not showing at all in the output tab ("transposed_data").

+ 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. Easy Way To Transpose 3 Colums of Data?
    By wings1080 in forum Excel General
    Replies: 17
    Last Post: 02-03-2015, 04:56 PM
  2. transpose column to a range calculate and transpose to new table loop
    By moshro1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-02-2015, 10:52 AM
  3. [SOLVED] Easy Transpose Macro
    By ebdmbfan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2012, 03:38 PM
  4. Easy way to transpose address info from one long column into neatly sorted rows.
    By hhhava in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-28-2012, 03:26 AM
  5. [SOLVED] Transpose: How can I copy a list and transpose it but leaving 3 cells in between each item
    By cocolete in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-11-2012, 10:01 AM
  6. Replies: 2
    Last Post: 02-19-2007, 04:53 PM
  7. new user with easy question? not easy for me
    By speakeztruth in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 06-03-2005, 05:05 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