Merge Multiple Lines into One Cell Reference Noncontiguous Range

Luke7731

New Member
Joined
Feb 13, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have two columns, one with the case number, and the other with varying amount of rows of related case notes (so varying amount of blank rows under each case number). Hundreds of cases, thousands of rows of case notes.
I’ve attached an image of what I have vs what I need. I need the case notes to remain one line on top of the next but I need all of the case notes associated with a case number to be in a single cell next to that case number cell.
So an automated way to merge multiple individual lines of text of various size into a single cell, grouped based on the case numbers to the side.

Thanks!
 

Attachments

  • IMG_2451 Medium.jpeg
    IMG_2451 Medium.jpeg
    117.6 KB · Views: 18

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this. It assumes your data is in columns A and B on Sheet1 and that you're compiling them onto columns A and B on Sheet2
I also assumed you wanted a line break between each note. I didn't mess with the column width of the compiled notes. You'll probably want it pretty wide

VBA Code:
Sub Compile()

Dim i, n As Integer
Dim c As Range

i = 0
n = 0
For Each c In Worksheets(1).Range("A1:A20")    'Modify this to include all the rows with data
If Not IsEmpty(c) Then
        i = i + 1
        n = n + 1
        Worksheets(2).Cells(i, 1) = c.Value
        Worksheets(2).Cells(i, 2) = c.Offset(0, 1).Value
    Else
        n = n + 1
'One of the next 2 lines has to be commented out
        Worksheets(2).Cells(i, 2) = Worksheets(2).Cells(i, 2).Value & vbCrLf & c.Offset(0, 1).Value   'This provides a line break between individual notes.
'        Worksheets(2).Cells(i, 2) = Worksheets(2).Cells(i, 2).Value & " " & c.Offset(0, 1).Value   'This provides a space between individual notes.
    End If
Next c

With Worksheets(2).UsedRange
    .VerticalAlignment = xlTop
    .HorizontalAlignment = xlLeft
End With

End Sub
 
Upvote 0
Hundreds of cases, thousands of rows of case notes.
Given the size of your data, I think this should be much faster.

VBA Code:
Sub CaseNotes()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 2)
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then
      k = k + 1
      b(k, 1) = a(i, 1)
      b(k, 2) = a(i, 2)
    Else
      b(k, 2) = b(k, 2) & vbLf & a(i, 2)
    End If
  Next i
  With Range("D1:E1").Resize(k)
    .Columns(2).ColumnWidth = 255
    .Value = b
    .Columns(2).WrapText = True
    .Columns.AutoFit
    .VerticalAlignment = xlTop
  End With
End Sub

Before:

Luke7731.xlsm
ABCDE
1Case 123Case notes 123
2Case notes 123
3Case notes 123
4Case notes 123
5Case notes 123
6Case notes 123
7Case 124Case notes 124
8Case notes 124
9Case notes 124
10Case notes 124
11Case 125Case notes 125
12Case 126Case notes 126
13Case notes 126
14Case notes 126
15Case notes 126
16Case notes 126
17Case notes 126
18Case notes 126
19Case notes 126
20Case notes 126
Sheet1


After: (Cols D:E only shown)

Luke7731.xlsm
DE
1Case 123Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123
2Case 124Case notes 124 Case notes 124 Case notes 124 Case notes 124
3Case 125Case notes 125
4Case 126Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126
Sheet1
 
Upvote 0
Solution
Given the size of your data, I think this should be much faster.

VBA Code:
Sub CaseNotes()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 2)
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then
      k = k + 1
      b(k, 1) = a(i, 1)
      b(k, 2) = a(i, 2)
    Else
      b(k, 2) = b(k, 2) & vbLf & a(i, 2)
    End If
  Next i
  With Range("D1:E1").Resize(k)
    .Columns(2).ColumnWidth = 255
    .Value = b
    .Columns(2).WrapText = True
    .Columns.AutoFit
    .VerticalAlignment = xlTop
  End With
End Sub

Before:

Luke7731.xlsm
ABCDE
1Case 123Case notes 123
2Case notes 123
3Case notes 123
4Case notes 123
5Case notes 123
6Case notes 123
7Case 124Case notes 124
8Case notes 124
9Case notes 124
10Case notes 124
11Case 125Case notes 125
12Case 126Case notes 126
13Case notes 126
14Case notes 126
15Case notes 126
16Case notes 126
17Case notes 126
18Case notes 126
19Case notes 126
20Case notes 126
Sheet1


After: (Cols D:E only shown)

Luke7731.xlsm
DE
1Case 123Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123
2Case 124Case notes 124 Case notes 124 Case notes 124 Case notes 124
3Case 125Case notes 125
4Case 126Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126
Sheet1
This worked wonderfully, thank you
 
Upvote 0
Try this. It assumes your data is in columns A and B on Sheet1 and that you're compiling them onto columns A and B on Sheet2
I also assumed you wanted a line break between each note. I didn't mess with the column width of the compiled notes. You'll probably want it pretty wide

VBA Code:
Sub Compile()

Dim i, n As Integer
Dim c As Range

i = 0
n = 0
For Each c In Worksheets(1).Range("A1:A20")    'Modify this to include all the rows with data
If Not IsEmpty(c) Then
        i = i + 1
        n = n + 1
        Worksheets(2).Cells(i, 1) = c.Value
        Worksheets(2).Cells(i, 2) = c.Offset(0, 1).Value
    Else
        n = n + 1
'One of the next 2 lines has to be commented out
        Worksheets(2).Cells(i, 2) = Worksheets(2).Cells(i, 2).Value & vbCrLf & c.Offset(0, 1).Value   'This provides a line break between individual notes.
'        Worksheets(2).Cells(i, 2) = Worksheets(2).Cells(i, 2).Value & " " & c.Offset(0, 1).Value   'This provides a space between individual notes.
    End If
Next c

With Worksheets(2).UsedRange
    .VerticalAlignment = xlTop
    .HorizontalAlignment = xlLeft
End With

End Sub
Thank you!
 
Upvote 0
Sorry--I misread your initial post. Here's a mini-sheet that explains the process. You can hide the substatus list column if you like, but you must keep it populated alongside your main tabulation data validation rows.

Cascading Dropdowns.xlsx
ABCDEF
1LeadStatusSubStatusSubstatus ListWon
2BLostLostBWon
3AWonWon
4NALostA
5NAThe client has already signed the offer (I arrived too late)
6NAUnsigned agreement
7NAThe client did not send any information
8NAChoose another offer
9NAThere is no solution for the client's request
10NA
11NALostB
12NAUnsigned agreement
13NAThe client did not send any information
14NAChoose another offer
15NAThere is no solution for the client's request
16NAThe client does not have a specific request for GrECo
17NA
18NAPending
19NAInsufficient contact details
20NAThe signed brokerage contract was awaited
21END OF TABLEWe are waiting for information from the client for the offer
22Offer sent
23We are waiting for a response to the offer
24A meeting with the client was requested
Sheet1
Cell Formulas
RangeFormula
D2:D20D2=IF(B2="Lost","Lost"&A2,IF(B2="Won","Won",IF(B2="Pending","Pending","NA")))
Cells with Data Validation
CellAllowCriteria
A2:A20ListA,B
B2:B20ListWon,Lost,Pending
C2:C20List=INDIRECT(D2)
 
Upvote 0
This worked wonderfully, thank you
So when I use it on a shorter sampe of my data it works great, but when I apply it to the whole dataset I get this error. Im not exactly sure what my problem is or how to go about fixing it. Lots of data accumulated by various secretaries over the years.
Thanks
 

Attachments

  • Screenshot EXCEL ERROR.png
    Screenshot EXCEL ERROR.png
    26.4 KB · Views: 8
  • Screenshot EXCEL.png
    Screenshot EXCEL.png
    24.8 KB · Views: 7
Upvote 0
Given the size of your data, I think this should be much faster.

VBA Code:
Sub CaseNotes()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
 
  a = Range("A1", Range("B" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 2)
  For i = 1 To UBound(a)
    If Len(a(i, 1)) > 0 Then
      k = k + 1
      b(k, 1) = a(i, 1)
      b(k, 2) = a(i, 2)
    Else
      b(k, 2) = b(k, 2) & vbLf & a(i, 2)
    End If
  Next i
  With Range("D1:E1").Resize(k)
    .Columns(2).ColumnWidth = 255
    .Value = b
    .Columns(2).WrapText = True
    .Columns.AutoFit
    .VerticalAlignment = xlTop
  End With
End Sub

Before:

Luke7731.xlsm
ABCDE
1Case 123Case notes 123
2Case notes 123
3Case notes 123
4Case notes 123
5Case notes 123
6Case notes 123
7Case 124Case notes 124
8Case notes 124
9Case notes 124
10Case notes 124
11Case 125Case notes 125
12Case 126Case notes 126
13Case notes 126
14Case notes 126
15Case notes 126
16Case notes 126
17Case notes 126
18Case notes 126
19Case notes 126
20Case notes 126
Sheet1


After: (Cols D:E only shown)

Luke7731.xlsm
DE
1Case 123Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123
2Case 124Case notes 124 Case notes 124 Case notes 124 Case notes 124
3Case 125Case notes 125
4Case 126Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126
Sheet1
So when I use it on a shorter sampe of my data it works great, but when I apply it to the whole dataset I get this error. Im not exactly sure what my problem is or how to go about fixing it. Lots of data accumulated by various secretaries over the years.
Thanks
 

Attachments

  • Screenshot EXCEL ERROR.png
    Screenshot EXCEL ERROR.png
    26.4 KB · Views: 9
  • Screenshot EXCEL.png
    Screenshot EXCEL.png
    24.8 KB · Views: 9
Upvote 0
Could you have error values in your data?

When you get the error, click Debug and then hover over the variable i:
1676882491246.png

Whatever number shows in the pop-up, go to that row in your data and look at the value in column B of that row.
 
Upvote 0
I was playing with a formula:
Book1
ABCDE
1123Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123 Case notes 123
2124Case notes 124 Case notes 124 Case notes 124 Case notes 124
3125Case notes 125
4126Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126 Case notes 126
5123Case notes 123
6Case notes 123
7Case notes 123
8Case notes 123
9Case notes 123
10Case notes 123
11124Case notes 124
12Case notes 124
13Case notes 124
14Case notes 124
15125Case notes 125
16126Case notes 126
17Case notes 126
18Case notes 126
19Case notes 126
20Case notes 126
21Case notes 126
22Case notes 126
23Case notes 126
24Case notes 126
Sheet1
Cell Formulas
RangeFormula
D1:E4D1=LET(a,$A$5:$A$24,b,$B$5:$B$24,c,ROW(a),u,UNIQUE(LOOKUP(ROW(a),IF(LEN(a),ROW(a)), a)),HSTACK(u,BYROW(u,LAMBDA(br,TEXTJOIN(CHAR(10),,FILTER(b,LOOKUP(c,IF(LEN(a),c), a)=br))))))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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