Transposing rows in first sheet into a single column in second sheet until end of data in first sheet

dportnoy

New Member
Joined
Apr 12, 2022
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
Hi. I am very new to VBA code and this loop crashed Excel several times for me.

I am attempting to transpose data from the range D2:K2 (which is in rows) in the "GoogleFormData" sheet to cell E22 in the "TheModel" sheet until it reaches a cell range Di:Kj with no data.

Right now, the data looks like this (this is data in columns D through K in the "GoogleFormData" sheet):

Screenshot 2022-04-12 021208.png


And I would like it to look like this (this is data is in column E in the "TheModel" sheet starting with E22 as the top cell):

Screenshot 2022-04-12 021231.png



This is the code I have attempted below.


VBA Code:
Sub TransposeModules()
    
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    
    i = 1
    j = 1
    k = 14
    
    Do Until IsEmpty(Worksheets("GoogleFormData").Range("D" & i & ":K" & j))
        
        i = i + 1
        j = j + 1
        k = k + 8
        
        Sheets("GoogleFormData").Select
        Range("D" & i & ":K" & j).Select
        Selection.Copy
        Sheets("TheModel").Select
        Range("E" & k).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
        
    Loop


End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi dportnoy,

Welcome to MrExcel!!

See how this goes:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsFrom As Worksheet, wsTo As Worksheet
    Dim lngRowTo As Long, lngRow As Long, lngPasteRow As Long
    
    Application.ScreenUpdating = False
    
    Set wsFrom = ThisWorkbook.Sheets("GoogleFormData")
    Set wsTo = ThisWorkbook.Sheets("TheModel")
    
    lngRowTo = wsFrom.Range("D:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngRow = 2 To lngRowTo
        lngPasteRow = IIf(lngPasteRow = 0, 22, wsTo.Cells(Rows.Count, "E").End(xlUp).Row + 1)
        wsFrom.Range("D" & lngRow & ":K" & lngRow).Copy
        wsTo.Range("E" & lngPasteRow).PasteSpecial Transpose:=True
    Next lngRow
    
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
    
End Sub

Regards,

Robert
 
Upvote 0
Solution
Hi dportnoy,

Welcome to MrExcel!!

See how this goes:

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsFrom As Worksheet, wsTo As Worksheet
    Dim lngRowTo As Long, lngRow As Long, lngPasteRow As Long
   
    Application.ScreenUpdating = False
   
    Set wsFrom = ThisWorkbook.Sheets("GoogleFormData")
    Set wsTo = ThisWorkbook.Sheets("TheModel")
   
    lngRowTo = wsFrom.Range("D:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   
    For lngRow = 2 To lngRowTo
        lngPasteRow = IIf(lngPasteRow = 0, 22, wsTo.Cells(Rows.Count, "E").End(xlUp).Row + 1)
        wsFrom.Range("D" & lngRow & ":K" & lngRow).Copy
        wsTo.Range("E" & lngPasteRow).PasteSpecial Transpose:=True
    Next lngRow
   
    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With
   
End Sub

Regards,

Robert
Thank you so much!!!!! It worked perfectly
 
Upvote 0
One way:
VBA Code:
Option Explicit
Sub test()
Dim rng As Range, cell As Range, arr(), k&
Set rng = Range("D2").CurrentRegion
ReDim arr(1 To rng.Count, 1 To 1)
    For Each cell In rng
        k = k + 1
        arr(k, 1) = cell.Value
    Next
Range("E22").Resize(k).Value = arr
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top