Data conversion from different rows to a single row for the same data input in the column A

Nagoo

New Member
Joined
Jul 2, 2015
Messages
30
Hi I am new to excel vba, I have data in the below format where there are multiple values against a single entry in A column. I wish to bring all the values against the same value in A column to its right. and avoid duplication.

Can you pls help me with a code?

Input data:
A
134​
A
12345​
A
567​
A
990​
B
123​
22​
33​
B
444​

Expected Output
A
134​
12345​
567​
990​
B
123​
22​
33​
444​

Thanks for the prompt support.
 

Attachments

  • expected output.jpg
    expected output.jpg
    9.3 KB · Views: 3
  • data input.jpg
    data input.jpg
    12.3 KB · Views: 3

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Is your data already sorted by the first column (so all the values that need to be on the same line are "lumped together consecutively", like in your original image?
What row does your data start on?
 
Upvote 0
OK, assuming that your data is already sorted to start, and your data begins in cell A2, this code should do what you want:
VBA Code:
Sub MyFormatData()

    Dim lr As Long
    Dim r As Long
    Dim lc As Long
    
    Application.ScreenUpdating = False
    
'   Find last row with data in columnn A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through range backwards
    For r = lr To 2 Step -1
'       see if value in column A is the same as the row above it
        If Cells(r, "A") = Cells(r - 1, "A") Then
'           Find last populated column in current row
            lc = Cells(r, Columns.Count).End(xlToLeft).Column
'           Copy values to row above
            Range(Cells(r, 2), Cells(r, lc)).Cut Cells(r - 1, Columns.Count).End(xlToLeft).Offset(0, 1)
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Sub MyFormatData() Dim lr As Long Dim r As Long Dim lc As Long Application.ScreenUpdating = False ' Find last row with data in columnn A lr = Cells(Rows.Count, "A").End(xlUp).Row ' Loop through range backwards For r = lr To 2 Step -1 ' see if value in column A is the same as the row above it If Cells(r, "A") = Cells(r - 1, "A") Then ' Find last populated column in current row lc = Cells(r, Columns.Count).End(xlToLeft).Column ' Copy values to row above Range(Cells(r, 2), Cells(r, lc)).Cut Cells(r - 1, Columns.Count).End(xlToLeft).Offset(0, 1) ' Delete row Rows(r).Delete End If Next r Application.ScreenUpdating = True End Sub
Thanks for the code - unfortunately the data is not sorted. A might once again repeat after B and vice versa as well. Appologies for not mentioning earlier.
Could u pls consider this as well and provide codes?
 
Upvote 0
Is your data already sorted by the first column (so all the values that need to be on the same line are "lumped together consecutively", like in your original image?
What row does your data start on?
1. DAta is not sorted.
2. Data starts from 2nd row. (Cell A2)
 
Upvote 0
This should do what you want:
VBA Code:
Sub MyFormatData()

    Dim lr As Long
    Dim r As Long
    Dim lc As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False
    
'   Find last row with data in columnn A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Build range to sort
    Set rng = Range("A2:B" & lr)
    
'   Sort range by column A
    rng.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlNo, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom
    
'   Loop through range backwards
    For r = lr To 2 Step -1
'       see if value in column A is the same as the row above it
        If Cells(r, "A") = Cells(r - 1, "A") Then
'           Find last populated column in current row
            lc = Cells(r, Columns.Count).End(xlToLeft).Column
'           Copy values to row above
            Range(Cells(r, 2), Cells(r, lc)).Cut Cells(r - 1, Columns.Count).End(xlToLeft).Offset(0, 1)
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hi, The last code does not work properly.

Data is getting mismatched. I believe during sorting. It reports an issue. Where only few cells are shifting during sort. Pls check :)

I think it sorts only till B column
 
Upvote 0
It works fine for me. I suspect maybe you have some sort of data issue, or something else you haven't told us (like errors in the data, formulas, in some cells, merged cells, etc).

Please upload a desensitized copy of your data to a file sharing site and provide a link to it here, so we can test it out on the exact same data you are trying.
 
Upvote 0
Thanks for the codes.

I tried editing one code: It resulted very well. Which is:

Set rng = Range("A2:XFD" & lr)
 
Upvote 0
Thanks for the codes.

I tried editing one code: It resulted very well. Which is:

Set rng = Range("A2:XFD" & lr)
OK, I see. Yes, that line was just limiting to the first two columns. Sorry, should have gone out further.
 
Upvote 0

Forum statistics

Threads
1,215,088
Messages
6,123,057
Members
449,091
Latest member
ikke

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