Help with duplicate values move horizontally

sakis_s

New Member
Joined
Sep 22, 2019
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
Hi!
I'm looking for a formula to do the following:

I have:

A
B
C
D
E
F
G
H
I
1
2040899

<colgroup><col width="68"></colgroup><tbody>
</tbody>
54Z4

<colgroup><col width="68"></colgroup><tbody>
</tbody>
2
2037840

<colgroup><col width="68"></colgroup><tbody>
</tbody>
3185

<colgroup><col width="68"></colgroup><tbody>
</tbody>
3
2037840

<colgroup><col width="68"></colgroup><tbody>
</tbody>
4455

<colgroup><col width="68"></colgroup><tbody>
</tbody>
4
2037840

<colgroup><col width="68"></colgroup><tbody>
</tbody>

<colgroup><col width="68"></colgroup><tbody>
7860

<colgroup><col width="68"></colgroup><tbody>
</tbody>
</tbody>
5
2038328

<colgroup><col width="68"></colgroup><tbody>
</tbody>
3123

<colgroup><col width="68"></colgroup><tbody>
</tbody>
6
2038328
9999

<colgroup><col width="68"></colgroup><tbody>
</tbody>
7
2038190

<colgroup><col width="68"></colgroup><tbody>
</tbody>
14B3

<colgroup><col width="68"></colgroup><tbody>
</tbody>
8
9

<tbody>
</tbody>

And i need the following result:

A
B
C
D
E
F
G
H
I
1
2040899
54Z4
2
2037840
3185
4455
7860
3
2038328
3123
9999
4
2038190
14B3
5
6
7
8
9

<tbody>
</tbody>

Removing duplicates from column A and entering Column B values from removed duplicates horizontally next to the first value.

Anyone has any idea how to do that?

Your help is much appreciated.

Thank you very much for your time.
 
Try making this change near the end of the macro
Rich (BB code):
<del>Range("A1").Resize(k, 2).Value = b</del>
With Range("A1").Resize(k, 2)
  .NumberFormat = "@"
  .Value = b
End With

I tried this one but it doesn't work with this script :whistle:

Code:
Sub Rearrange()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
      d(a(i, 1)) = d(a(i, 1)) & ";" & a(i, 2)
  Next i
  [COLOR=#ff0000]With Range("D2:E2").Resize(d.Count)
  .NumberFormat = "@"
  .Value = Application.Transpose(Array(d.Keys, d.Items))
End With[/COLOR]
  Range("E2").Resize(d.Count).TextToColumns DataType:=xlDelimited, Semicolon:=True, FieldInfo:=Array(Array(1, 9))
End Sub
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I tried this one but it doesn't work with this script :whistle:
Try this one
Code:
Sub Rearrange_v2()
  Dim d As Object
  Dim a As Variant, vFieldInfo As Variant
  Dim i As Long, NumCols As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
      d(a(i, 1)) = d(a(i, 1)) & ";" & a(i, 2)
  Next i
  With Range("D1:E1").Resize(d.Count)
    .Value = Application.Transpose(Array(d.Keys, d.Items))
    NumCols = Evaluate(Replace("aggregate(14,6,len(#)-len(substitute(#,"";"","""")),1)", "#", .Columns(2).Address)) + 1
    ReDim vFieldInfo(1 To NumCols)
    vFieldInfo(1) = Array(1, 9)
    For i = 2 To NumCols
      vFieldInfo(i) = Array(i, 2)
    Next i
    .Columns(2).TextToColumns DataType:=xlDelimited, Semicolon:=True, FieldInfo:=vFieldInfo
  End With
End Sub
 
Upvote 0
Try this one
Code:
Sub Rearrange_v2()
  Dim d As Object
  Dim a As Variant, vFieldInfo As Variant
  Dim i As Long, NumCols As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a)
      d(a(i, 1)) = d(a(i, 1)) & ";" & a(i, 2)
  Next i
  With Range("D1:E1").Resize(d.Count)
    .Value = Application.Transpose(Array(d.Keys, d.Items))
    NumCols = Evaluate(Replace("aggregate(14,6,len(#)-len(substitute(#,"";"","""")),1)", "#", .Columns(2).Address)) + 1
    ReDim vFieldInfo(1 To NumCols)
    vFieldInfo(1) = Array(1, 9)
    For i = 2 To NumCols
      vFieldInfo(i) = Array(i, 2)
    Next i
    .Columns(2).TextToColumns DataType:=xlDelimited, Semicolon:=True, FieldInfo:=vFieldInfo
  End With
End Sub

Good morning Peter!
I tried it but this bring all results in one cell separated with ";"
11.jpg


I could use "text to columns" now but it's a time-consuming process. Any ideas to fix this? Thank you!
 
Upvote 0
Also forgot to mention that i'm receiving the following error when i run it:

Capture2.jpg
 
Upvote 0
I tried it but this bring all results in one cell separated with ";"
It is supposed to do that because the next thing my code does is Text To Columns, but clearly that is not happening due to the error that you have now also reported.

As yet I have not been able to reproduce the problem.

1. Have you changed the code in any way to adapt to your particular circumstances? If so, please post the code you are now using.

2. What version of Excel and what operating system are you using?

3. Are you able to upload a small dummy file (any sensitive data removed) that has this problem to a file-share site (eg DropBox) and provide a link to the file so we can have a look at it?
 
Upvote 0
It is supposed to do that because the next thing my code does is Text To Columns, but clearly that is not happening due to the error that you have now also reported.

As yet I have not been able to reproduce the problem.

1. Have you changed the code in any way to adapt to your particular circumstances? If so, please post the code you are now using.

2. What version of Excel and what operating system are you using?

3. Are you able to upload a small dummy file (any sensitive data removed) that has this problem to a file-share site (eg DropBox) and provide a link to the file so we can have a look at it?

How silly i am! I'm using first row to keep notes and i forgot to change cells A1, D1 & E1 to A2, D2 & E2. Sometimes solution is the most obvious. :eek:
Thank you so much Peter you really helped a lot! All best! (y)
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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