Excel - How to Create VBA FOR Loop From Macro Code

Excel 2013
Windows 7

I have over 1,000 data records. In each record, one of the columns, (E in this example, but it could change), contains three rows of data that need to be transposed to three adjacent columns. But there are also columns that need one or four rows transposed. Assume that I've already created the blank columns where the transposition will be pasted.

Keep in mind that other columns contain data that must not be disturbed.

I need help to create the VB code of a FOR (or WHILE) loop to perform the action represented by the macro I recorded. I assume a blank row will terminate the loop.

Assume the original data is in this format (please paste as-is into your worksheet) -
Name1	(blank)	(blank)
123 Street Blvd	(blank)	(blank)
City, State	(blank)	(blank)
Name2	(blank)	(blank)
123 Street Blvd	(blank)	(blank)
City, State	(blank)	(blank)
Name3	(blank)	(blank)
123 Street Blvd	(blank)	(blank)
City, State	(blank)	(blank)
(blank)	(blank)	(blank)

Open in new window


The result should look like this (paste into your worksheet) -
Name1	123 Street Blvd	City, State
(blank)	(blank)	(blank)
(blank)	(blank)	(blank)
Name2	123 Street Blvd	City, State
(blank)	(blank)	(blank)
(blank)	(blank)	(blank)
Name3	123 Street Blvd	City, State
(blank)	(blank)	(blank)
(blank)	(blank)	(blank)
(blank)	(blank)	(blank)

Open in new window


This is the code that was generated by the macro when I recorded the keystrokes.
Sub Transpose_2_Rows_to_2_Columns()
'
' Transpose_2_Rows_to_2_Columns Macro
'
' Keyboard Shortcut: Ctrl+t
'
    Range("E16:E17").Select
    Selection.Copy
    Range("F15").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("E19:E20").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("F18").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
    Range("E22:E23").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("F21").Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
End Sub

Open in new window

Jerry LAsked:
Who is Participating?
 
Bill PrewCommented:
Here's an adjusted version that does the copy out to the right, transposing, and then removes the rows where the data was moved from.  It also looks dynamically to find the "bottom" of the data to process all of it.

Sub Transpose_2_Rows_to_2_Columns()

    ' Define Column to get data from, column to paste data to, and top row of the data
    strFromCol = "E"
    strToCol = "F"
    intTopRow = 1
    
    ' Determine last row of the data
    intLastRow = Cells(65536, strFromCol).End(xlUp).Row

    ' Loop through data, botton to top (for deletes), moving 2nd and 3rd entries
    For intRow = intLastRow - 2 To intTopRow Step -3
        ' Transpose 2nd and 3rd cells to right of first cell
        Range(strFromCol & (intRow + 1) & ":" & strFromCol & (intRow + 2)).Select
        Selection.Copy
        Range(strToCol & intRow).Select
        Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        
        ' Delete rows where data was moved from
        Rows(intRow + 1).Delete
        Rows(intRow + 1).Delete
    Next
    
End Sub

Open in new window

~bp
0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
It would look something like this:
 
for introw = 15 to 999 step 3
    Range("E" & (introw+1) & ":E" & (introw+2)).Select
    Selection.Copy
    Range("F" & introw).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=True
next
0
 
KimputerCommented:
My code looks like this (assumes Name1 is really at cell 1,1 (A1) and they always are in 3 rows (no spaces, no empty rows):

Sub transpose()
    Set cs = ActiveSheet
    For i = 1 To cs.UsedRange.Rows.Count Step 1
        If Not ((i Mod 3) = 1) Then
            If ((i Mod 3) = 2) Then
                cs.Cells(i - 1, 2).Value = cs.Cells(i, 1).Value
                cs.Cells(i, 1).Value = ""
            Else
                If ((i Mod 3) = 0) Then
                    cs.Cells(i - 2, 3).Value = cs.Cells(i, 1).Value
                    cs.Cells(i, 1).Value = ""
                End If
            End If
        End If
    
    Next
End Sub

Open in new window


If satisfactory, later an option could be added to have a prompt, which column to choose instead of A.
0
 
Jerry LAuthor Commented:
Thanks everyone for your input.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.