# need a macro to convert rows to columns with cell reverence

#### Lavan

##### Board Regular
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

Lavan

### Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

#### rbrhodes

##### Board Regular
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

'cleanup
Set Cel = Nothing
Set Rng = Nothing

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

End Sub

#### Lavan

##### Board Regular
dr, Thanks a million.

-Lavan

#### rbrhodes

##### Board Regular
Hi Lavan

I take it works then? <G>

You're welcome!

#### Lavan

##### Board Regular
Yes dr, Its a perfect match to my requirement. Thanks again.

Lavan

Replies
7
Views
325
Replies
0
Views
100
Replies
0
Views
145
Replies
1
Views
663
Replies
2
Views
242

1,186,911
Messages
5,960,543
Members
438,483
Latest member
Shahin Jack

### 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.

### Which adblocker are you using?

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

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