Results 1 to 10 of 10

Transpose Column Data to Rows

Threaded View

  1. #1
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Transpose Column Data to Rows

    Hi,

    Below mentioned code is extracted from my previous Post code provided by forum contributor "WideBoyDixon"-Thanx a lot to him.

    The changes that I want is to get a revise the code that produce the output/result as mentioned in "Expected" worksheet.Slight changes are there than that of previous output which has been reflected in"Existing Code Output".Since,I heavily relied on Power Query but have no Code No.in C column those list is not displayed.Hence,now I have to go with this below code.
    Public Sub TransposeData()
    
    Dim lastRow As Long
    Dim thisRow As Long
    Dim nextRow As Long
    Dim foundCol As Variant
    Dim nextCol As Long
    Dim lastString As String
    
    Dim dataSheet As Worksheet
    Dim tranSheet As Worksheet
    
    Set dataSheet = Worksheets("Data")
    Set tranSheet = Worksheets.Add(After:=dataSheet)
    tranSheet.Name = "Transposed"
    
    lastRow = dataSheet.Cells(dataSheet.Rows.Count, "B").End(xlUp).Row
    nextRow = 1
    nextCol = 5
    tranSheet.Range("1:1").Font.Bold = True
    tranSheet.Range("C1").Value = dataSheet.Range("D3").Value
    lastString = ""
    
    For thisRow = 3 To lastRow
        foundCol = Application.Match(dataSheet.Cells(thisRow, "D").Value, tranSheet.Range("1:1"), 0)
        If IsError(foundCol) Then
            foundCol = nextCol
            tranSheet.Cells(1, foundCol).Value = dataSheet.Cells(thisRow, "D").Value
            nextCol = nextCol + 1
        End If
        If dataSheet.Cells(thisRow, "C").Value <> lastString Then
            lastString = dataSheet.Cells(thisRow, "C").Value
            nextRow = nextRow + 1
        End If
        tranSheet.Cells(nextRow, foundCol).Value = dataSheet.Cells(thisRow, "B").Value
        tranSheet.Cells(nextRow, "D").Value = dataSheet.Cells(thisRow, "C").Value
    Next thisRow
    
    tranSheet.Range(tranSheet.Columns(3), tranSheet.Columns(nextCol)).EntireColumn.AutoFit
    
    End Sub
    Also to create in a existing userdefined "Expected" worksheet properly formatted adjusted width rather than creating result in a New worksheet.
    Hoping a positive response.
    Attached Files Attached Files
    Last edited by paradise2sr; 07-11-2019 at 08:32 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. To transpose or not to transpose? What decides? Table / Array?
    By LIL2606 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-19-2019, 01:28 PM
  2. Replies: 4
    Last Post: 08-21-2018, 09:24 AM
  3. [SOLVED] Use the TRANSPOSE() function to transpose a range as an input to the UDF
    By alice2011 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-09-2015, 09:35 PM
  4. 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
  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. I WANT TO TRANSPOSE LINKS, AS WE TRANSPOSE VALUES
    By Umair Aslam in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-22-2005, 09:05 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