0

I have the below code to transpose the first cell of each row onto a single row, but I'm not sure how to amend it so that it takes the first 5 cells of each row and does the same?

ie I have this data in rows A-E -

enter image description here

and the code will copy the dates in column A as below -

enter image description here

But what I would like is the data transposed as below and so on. It also creates a new worksheet to transpose onto, what I would ideally like is for it to copy to an existing sheet (for arguments sake Sheet1) -

enter image description here

Option Explicit

Sub Transpose_List()

    Dim WS As Worksheet
    Dim WSA As Worksheet
    Dim NextRow As Long
    Dim LastRow As Long
    Dim i As Long

    Set WSA = ActiveSheet

    Set WS = ActiveWorkbook.Sheets.Add(Before:=Worksheets(Worksheets.Count))
    WS.Name = "Transposed"

    LastRow = WSA.Cells(Rows.Count, 1).End(xlUp).row

    Application.ScreenUpdating = False

    For i = 5 To LastRow Step 6
        NextRow = WS.Cells(Rows.Count, 1).End(xlUp).row + 1
        WSA.Cells(i, 1).Resize(6, 1).Copy
        WS.Cells(NextRow, 1).PasteSpecial Transpose:=True
    Next i

    WS.Columns("A:F").AutoFit

    Application.ScreenUpdating = True

End Sub
5
  • 2
    Take your data into array and then paste in the new sheet as you wish Commented Sep 15, 2023 at 10:31
  • 1
    A formula would work: =TEXTSPLIT(TEXTJOIN(",",FALSE,A5:E10),",") Commented Sep 15, 2023 at 10:35
  • @CLR I'd ideally like to do this through VBA if possible! Commented Sep 15, 2023 at 10:55
  • 1
    What happened when you changed your code to paste into an existing sheet instead of creating a new sheet? Or do you not understand the code you are using. I would think all you need to do is to Set WS to an existing workbook/worksheet and remove the code that is creating the new sheet. Commented Sep 15, 2023 at 11:09
  • You could use VBA (as you state you want to use that) to write that formula to a cell. I guess there's more to your requirement that you've not put here, as a single cell with a single formula is all you need. Commented Sep 15, 2023 at 12:28

1 Answer 1

0

Target row is the row in the Sheet1 to transpose array. (Set to desired value)

ActiveSheet has to be the source sheet.

Sub transi()
targetrow = 3
For i = 5 To ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    ActiveSheet.Range("A" & i & ":E" & i).Copy Worksheets("Sheet1").Cells(targetrow, 1 + (i - 5) * 5)
Next i
End Sub

If want to remove the range from the source sheet, change Copy to Cut.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! If I wanted to amend this to copy A-E, but then also include U-V, what would I need to change?
I have tried the below, but it only adds on U-V for the final row, not the preceding ones also. ActiveSheet.Range("A" & i & ":E" & i & ",U" & i & ":V" & i).Copy Worksheets("Monthly CSV").Cells(targetrow, 1 + (i - 5) * 5)
Never mind, I got there in the end! ActiveSheet.Range("A" & i & ":E" & i & ",U" & i & ":V" & i).Copy Worksheets("Monthly CSV").Cells(targetrow, 1 + (i - 5) * 7)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.