VBA Transpose Data and Insert Rows

SMITEE

New Member
Joined
Oct 4, 2016
Messages
3
Hello, I want a macro that will look at columns F through K and:
  1. If a cell in those columns contains a value, then insert a row after
  2. Transpose the value to column E
  3. (Optional) auto-fill columns A, B, C, and D with the preceeding value

Note that if a cell in columns F-K has a value, then the corresponding cell in the preceeding column always has a value.

Before:
ABCDEFGHIJK
100abcfallbluehandbackhip
200defspringredthighhead
300ghiwintergreenarmneckshoulderwristelbowkneeankle
400jklsummeryellowabdomen

<tbody>
</tbody>

After:
ABCDEFGHIJK
100abcfallbluehand
100abcfallblueback
100abcfallbluehip
200defspringredthigh
200defspringredhead
300ghiwintergreenarm
300ghiwintergreenneck
300ghiwintergreenshoulder
300ghiwintergreenwrist
300ghiwintergreenelbow
300ghiwintergreenknee
300ghiwintergreenankle
400jklsummeryellowabdomen

<tbody>
</tbody>
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,100
try this on a copy of your file,

Code:
Sub do_it()

lr = Range("A" & Rows.Count).End(xlUp).Row
sr = lr + 2

For r = 1 To lr

a = Cells(r, "A")
b = Cells(r, "B")
c = Cells(r, "C")
d = Cells(r, "D")


For x = 5 To Cells(r, Columns.Count).End(xlToLeft).Column

Cells(sr, "A") = a
Cells(sr, "B") = b
Cells(sr, "C") = c
Cells(sr, "D") = d
Cells(sr, "E") = Cells(r, x)
sr = sr + 1

Next x
Next r
End Sub
hth,

Ross
 

SMITEE

New Member
Joined
Oct 4, 2016
Messages
3
Hi Ross, I'm afraid that didn't work. I ran the macro and there were no changes to my worksheet.
 

SMITEE

New Member
Joined
Oct 4, 2016
Messages
3
I tried fiddling with the following macro without success.

Sub TransposeSpecial()
Dim lMaxRows As Long 'max rows in the sheet
Dim lThisRow As Long 'row being processed
Dim iMaxCol As Integer 'max used column in the row being processed


lMaxRows = Cells(Rows.Count, "E").End(xlUp).Row

lThisRow = 1 'start from row 1

Do While lThisRow < lMaxRows

iMaxCol = Cells(lThisRow, Columns.Count).End(xlToLeft).Column

If (iMaxCol > 1) Then
Rows(lThisRow + 1 & ":" & lThisRow + iMaxCol - 1).Insert
Range(Cells(lThisRow, 2), Cells(lThisRow, iMaxCol)).Copy
Range("E" & lThisRow + 1).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
Range(Cells(lThisRow, 2), Cells(lThisRow, iMaxCol)).Clear
lThisRow = lThisRow + iMaxCol - 1
lMaxRows = Cells(Rows.Count, "E").End(xlUp).Row
End If

lThisRow = lThisRow + 1
Loop
End Sub
Sub Test()


End Sub
 

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,100
I tested my code on a different computer and it worked. can anyone else please test it. and let me know.

Thanks,
Ross
 

Watch MrExcel Video

Forum statistics

Threads
1,098,863
Messages
5,465,120
Members
406,414
Latest member
Discorz

This Week's Hot Topics

Top