I have an Excel sheet that looks like the first picture and I want to convert it to the second picture:
I have written the following code but it does not work as expected. It deletes more rows than expected. What's wrong with the code?
Sub Trans3()
Dim rng As Range, rng2 As Range
Dim I As Long
Dim J As Integer, Z As Integer, Q As Integer, T As Integer
Set rng = Range("B1")
While rng.Value <> ""
For Each y In Range("A1:A10")
I = I + 1
J = I
Z = 1
Do While Cells(J + 1, 1).Value = Cells(J, 1).Value
J = J + 1
Loop
Set rng2 = Range("B" & I & ":B" & J)
If I > 1 Then
Z = J - I + 1
Else
Z = J
End If
rng2.Resize(Z).Copy
Range("C" & I).PasteSpecial Transpose:=True
T = I
Do While J > 1
Q = T + 1
Rows(Q).EntireRow.Delete
J = J - 1
Loop
Next y
Wend
End Sub
Shank, using your code, I've made minor modifications and now it deletes the right number of rows and it works, try it out.
My take on this problem .
So I did a little refactoring. I moved everything into arrays to speed it up.
See notes in code for reference.
This does require that the data be sorted on column A.