Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , I have a similar problem relating to transposing rows to columns wrt to the above.

I applied the VBA code I could only get 1 column instead if 3 columns.

My data table format is like below :
CTR   22/2/22
CTR   28/2/22
CTR   31/3/22

I have over 5000 number of rows to deal with.
I want to able to view as below:
CTR   22/2/22   28/2/22   31/3/22 for various documents

Please can any one help out here?
I'm new to excel VBA.

What I have tried:

VBA
Sub Macro1()
    Dim prevRow As Integer
    Dim currentRow As Integer
    Dim strTemp As String
    Dim pasteColumnIdx As Integer
    
    prevRow = 2 '1st row is for column heading
    currentRow = prevRow + 1 'starts from row 3
    pasteColumnIdx = 8
    
    Do While Cells(currentRow, 1).Text <> ""
    
        If Cells(prevRow, 1).Text = Cells(currentRow, 1).Text Then
            Range(Cells(currentRow, 4), Cells(currentRow, 7)).Select
            Selection.Cut
            Range(Cells(prevRow, pasteColumnIdx), Cells(prevRow, pasteColumnIdx)).Select
            pasteColumnIdx = pasteColumnIdx + 4
            ActiveSheet.Paste
            strTemp = CStr(currentRow) + ":" + CStr(currentRow)
            Rows(strTemp).Select
            Selection.Delete Shift:=xlUp
            'currentRow = currentRow + 1
        Else
            prevRow = prevRow + 1
            currentRow = currentRow + 1
            pasteColumnIdx = 8
        End If
Posted
Updated 17-Jan-22 1:05am
v2

You should avoid using Copy and Paste - if something else takes the focus while you are doing that then the data will not be pasted into your work book. It's not a robust method and can take a long time.

You should convert the Range of data into an array, transpose the array and then set the value of an appropriate Range to that transposed array.

For example if you have a spreadsheet set up like this
Col A Col B
CTR   22/2/22
CTR   28/2/22
CTR   31/3/22
Use this code to change it to
Col A Col B     Col C     Col D
CTR   22/2/22   28/2/22   31/3/22
and to get rid of the rest of the data
VB
Public Sub demo()

    'Determine how many rows of data you have - that is the number of columns you will want
    Dim lRows As Long
    lRows = ThisWorkbook.Sheets(1).Cells(ThisWorkbook.Sheets(1).Rows.Count, "A").End(xlUp).Row
    
    'Get the data from the spreadsheet
    Dim arrDemo As Variant
    arrDemo = ThisWorkbook.Sheets(1).Range("B1:B" & CStr(lRows)).Value
    
    'Transpose Array
    Dim arrTransposed As Variant
    arrTransposed = Application.WorksheetFunction.Transpose(arrDemo)
      
    'Write the transposed array back to Excel leaving the first "CTR" in cell A1
    Range("B1").Resize(1, lRows).Value = arrTransposed

    'Clear out the rest of the data
    ThisWorkbook.Sheets(1).Range("A2:B" & CStr(lRows)).ClearContents

    
End Sub
 
Share this answer
 
Comments
Maciej Los 17-Jan-22 11:55am    
5ed!
If I understand the question correctly, one way to do this is to select the desired range as you have done, then copy it instead of cutting. After that, use Range.PasteSpecial method (Excel) | Microsoft Docs[^] with the Transpose option set to true.

Once that is done you can delete the rows you don't need anymore and move forward to the next area
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900