need a macro to convert rows to columns with cell reverence

Lavan

Board Regular
Joined
Dec 15, 2004
Messages
56
Hello Experts,

I need a macro to convert rows to columns with reverence to a cell value in column A. For example, I have a huge data like below

549 Peter
549 Peter
549 Bob
54B sam
54B sam
54D Mike
54D Mike
54D Mike
54F tony
54F tony
551 tony
551 tony
553 tony
553 tony
555 mike
555 mike
555 tom
555 tom
557 john
557 John
557 john
557 Peter
557 Peter

I want to move the data of B2 to C1 if A2=A1 and B3 to D1 if A3=A1 and so on.. till next cell value of A is not equal to A1 and start with new cell again. Finally it should be like below.

549 Peter Peter Bob
54B sam sam
54D Mike Mike Mike
54F tony tony
551 tony Tony
553 tony Tony
555 mike Mike tom tom
557 john John John Peter Peter


Thanks in Advance
Lavan
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Lavan.

Like this:

Option Explicit

Sub ToRows()

Dim Cel As Range
Dim Rng As Range
Dim cCount As Long
Dim rCount As Long
Dim NumCol As Long
Dim NamCol As Long
Dim LastRow As Long
Dim StartRow As Long

'BEGIN CHANGE
'col with numbers: CHANGE TO SUIT
NumCol = 1
'col with names: CHANGE TO SUIT
NamCol = 2
'first row of data :CHANGE TO SUIT
StartRow = 1
'END CHANGE

'get last row of data (numbers Column)
LastRow = Cells(65536, NumCol).End(xlUp).Row

'what to work on (numbers Column)
Set Rng = Range(Cells(StartRow, NumCol), Cells(LastRow, NumCol))

'speed, recursion
With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'init
cCount = NamCol + 1
rCount = StartRow

'do sort
With Rng
For Each Cel In Rng
With Cel
If .Offset(1, 0) = Cel Then
.Offset(1, 1).Cut Destination:=Cells(rCount, cCount)
cCount = cCount + 1
Else
cCount = NamCol + 1
rCount = Cel.Row + 1
End If
End With
Next Cel
End With

'kill blanks
Range(Cells(StartRow, NamCol).Address, Cells(LastRow, NamCol).Address).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

'cleanup
Set Cel = Nothing
Set Rng = Nothing

'reset
With Application
.ScreenUpdating = True
.EnableEvents = True
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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