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>
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
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
 
Upvote 0
Hi Ross, I'm afraid that didn't work. I ran the macro and there were no changes to my worksheet.
 
Upvote 0
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
 
Upvote 0
I tested my code on a different computer and it worked. can anyone else please test it. and let me know.

Thanks,
Ross
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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