VBA: Add Chr(10) into code when concatenating

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,362
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I posted a thread a while back and received some help for concatenating. This code worked great for what I was doing then and will work (I think) for this scenario, but instead of the "/" separating concatenated values, I would rather a line break. So, as you can see in row 14 and row 15, I'd like to see the M and N in column E to be separated by a line break instead of the "/". I tried placing a Chr(10) in the code, but it adds an extra space on top of the concatenated values. In this example it only show two duplicates, but there could be more than just two.

Calendar.xlsm
ABCDE
1NumberLetterNumberLetter
21--5A1--5A
31--22B1--22B
41--26C1--26C
52--4D2--4D
62--8E2--8E
72--12F2--12F
82--20G2--20G
92--23H2--23H
102--24I2--24I
112--26J2--26J
123--6K3--6K
133--14L3--14L
143--15M3--15M / N
153--15N3--16O
163--16O3--19P
173--19P3--21Q
183--21Q4--5R
194--5R4--20S
204--20S4--29T
214--29T5--4U / V
225--4U5--14W
235--4V5--24X
245--14W
255--24X
Sheet2
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:B25Cell ValueduplicatestextNO


VBA Code:
Sub Test()
  Dim R As Long, Data As Variant
  Data = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp))
  Sheets("Sheet2").Range("A1:B1").Copy Sheets("Sheet2").Range("D1:E1")
  With CreateObject("Scripting.Dictionary")
    For R = 1 To UBound(Data)
      .Item(Data(R, 1)) = .Item(Data(R, 1)) & " / " & Data(R, 2)
      If Left(.Item(Data(R, 1)), 3) = " / " Then .Item(Data(R, 1)) = Mid(.Item(Data(R, 1)), 4)
    Next
    Sheets("Sheet2").Range("D2").Resize(.Count) = Application.Transpose(.Keys)
    Sheets("Sheet2").Range("E2").Resize(.Count) = Application.Transpose(.Items)
  End With
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about
VBA Code:
Sub Test()
  Dim R As Long, Data As Variant
  Data = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Cells(Rows.Count, "B").End(xlUp))
  Sheets("Sheet2").Range("A1:B1").Copy Sheets("Sheet2").Range("D1:E1")
  With CreateObject("Scripting.Dictionary")
    For R = 1 To UBound(Data)
      If Not .Exists(Data(R, 1)) Then
         .Item(Data(R, 1)) = Data(R, 2)
      Else
         .Item(Data(R, 1)) = .Item(Data(R, 1)) & vbLf & Data(R, 2)
      End If
    Next
    Sheets("Sheet2").Range("D2").Resize(.Count) = Application.Transpose(.Keys)
    Sheets("Sheet2").Range("E2").Resize(.Count) = Application.Transpose(.Items)
  End With
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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