Join rows in to one cell

Clamarc

New Member
Joined
Apr 19, 2023
Messages
20
Office Version
  1. 2016
Platform
  1. Windows
My excel 2016 doesn't have the "textjoin" function, so I use a custom function that I called "joinlines", where the values of several lines of a column are joined in a single cell and separated by comma+space. The problem is that if there are hidden rows they are also returned in the join result and I also need to remove the VCI- and PO- from the beginning of the records.

The values in the rows i want to join are like this:
VCI-2130/17
VCI-2134.1/17
PO-2134.2/17

In my function, I managed to join the values, but I have two problems:
1 - I don't want to join the hidden rows
2 - I don't want the values from the beginning, ex. VCI- and PO-

And I wanted to merge into a single cell like this, without the VCI- and PO- at the beginning and only on the visible rows:
2130/17, 2134.1/17, 2134.2/17

See the function below:

VBA Code:
Public Function JOINLINES(vArray As Variant, _
Optional lSize As Long, _
Optional sSeparator As String = ", ") As Variant
Dim v As Variant
Dim lCount As Long
Dim asOut() As String
 
If lSize < 0 Then
JOINLINES = CVErr(xlErrNum)
Exit Function
End If
 
For Each v In vArray                                 '--.
If v <> "" Then                                            '| 
lCount = lCount + 1                                   '| here loads my array,
ReDim Preserve asOut(1 To lCount)           '| but i want to load [B]only[/B] 
asOut(lCount) = v                                       '| the [B]visible rows[/B] 
If lCount = lSize Then Exit For                    '| without the [B]VCI-[/B] and [B]PO-[/B]
End If                                                          '|
Next v                                                      '--.
 
JOINLINES = Join(asOut, sSeparator)
End Function


What should i change in the code to join only visible rows and the values without VCI- and PO- ?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi @Clamarc. Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Try if the following solves what you need:
VBA Code:
Public Function JOINLINES(vArray As Variant, _
      Optional lSize As Long, _
      Optional sSeparator As String = ", ") As Variant
  Dim v As Variant
  Dim lCount As Long
  Dim asOut() As String
   
  If lSize < 0 Then
    JOINLINES = CVErr(xlErrNum)
    Exit Function
  End If
   
  For Each v In vArray
    If v.EntireRow.Hidden = False Then
      If v <> "" Then
        lCount = lCount + 1
        ReDim Preserve asOut(1 To lCount)
        asOut(lCount) = Replace(Replace(v, "VCI-", "", , , vbTextCompare), "PO-", "", , , vbTextCompare)
        If lCount = lSize Then Exit For
      End If
    End If
  Next v
   
  JOINLINES = Join(asOut, sSeparator)
End Function


--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 1
Solution
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
Hi DanteLove
The code is working perfectly, but it only accepts values in a range A1:A12. How to merge, too, by selecting cells individually, eg. A1;A3;A4;A12...?

thanks
 
Upvote 0
How to merge, too, by selecting cells individually, eg. A1;A3;A4;A12...?

Put the cells or range of cells between quotes and within the quotes separated by commas, example:
Dante Amor
ABCD
1
2VCI-2130/172130/17, 2134.2/17, 2134.1/18, 2130/17, 2134.1/17
3VCI-2134.1/17
4PO-2134.2/17
5VCI-2130/18
6VCI-2134.1/18
7PO-2134.2/18
8VCI-2130/17
9VCI-2134.1/17
10PO-2134.2/17
11VCI-2130/18
12VCI-2134.1/18
13PO-2134.2/18
14PO-2134.2/18
15VCI-2130/17
16VCI-2134.1/17
17PO-2134.2/17
18VCI-2130/18
Hoja10
Cell Formulas
RangeFormula
D2D2=JOINLINES("B8,B10,B12,B15:C18",5,", ")


Try this:
VBA Code:
Public Function JOINLINES(vArray As Variant, _
      Optional lSize As Long, _
      Optional sSeparator As String = ", ") As Variant
  Dim v As Variant
  Dim lCount As Long
  Dim asOut() As String
  Dim rng As Range

  If lSize < 0 Then
    JOINLINES = CVErr(xlErrNum)
    Exit Function
  End If
  Set rng = Range(vArray)
  For Each v In rng
    If v.EntireRow.Hidden = False Then
      If v <> "" Then
        lCount = lCount + 1
        ReDim Preserve asOut(1 To lCount)
        asOut(lCount) = Replace(Replace(v, "VCI-", "", , , vbTextCompare), "PO-", "", , , vbTextCompare)
        If lCount = lSize Then Exit For
      End If
    End If
  Next v

  JOINLINES = Join(asOut, sSeparator)
End Function

🫡
 
Upvote 0
Try this:
VBA Code:
Public Function JOINLINES(vArray As Variant, _
      Optional lSize As Long, _
      Optional sSeparator As String = ", ") As Variant
  Dim v As Variant
  Dim lCount As Long
  Dim asOut() As String
  Dim rng As Range 
[/QUOTE]
It works, but do I always have to manually change the selected cells in the function bar?... if they were few lines, ok, but my file has more than 10,000 lines!

Eg. When I select the cells, the function bar shows JOINLINES(A1;A3;A12;A14:A18), so I have to edit the command and manually change it to JOINLINES("A1,A3,A12,A14:A18") so that work.

If I select 1,000 different lines I will have to manually put double quotes at the beginning and end, and then change 1,000 "[B];[/B]" to "[B],[/B]" is that it?
 
Upvote 0
Hi, DanteAmor,

It works, but do I always have to manually change the selected cells in the function bar?... if they were few lines, ok, but my file has more than 10,000 lines!

Eg. When I select the cells, the function bar shows JOINLINES(A1;A3;A12;A14:A18), so I have to edit the command and manually change it to JOINLINES("A1,A3,A12,A14:A18") so that work.

If I select 1,000 different lines I will have to manually put double quotes at the beginning and end, and then change 1,000 ";" to "," is that it?

thanks
 
Upvote 0
If I select 1,000 different lines
If you have to select 1000 cells one by one :unsure:, something is wrong with your process. Imagine, you go to the selection of cell 728 and you make a mistake or something happens, you must start from cell1, cell2... up to cell1000. :cry:


Forget the quotes double . What you should do is find a pattern that identifies out of the 10,000 lines which are the 1,000 lines you need, then you don't need to select one by one, the macro will identify those 1,000 lines and concatenate them. :cool:

So when you identify that pattern, you come back here, create a new post, for us to help you with a new function, of course, based on that pattern.
If you don't have a pattern, I'm afraid no function will help you avoid selecting those thousand cells one by one. ;)
 
Upvote 0
If you have to select 1000 cells one by one :unsure:, something is wrong with your process. Imagine, you go to the selection of cell 728 and you make a mistake or something happens, you must start from cell1, cell2... up to cell1000. :cry:...

There is no standard.

I think I didn't express myself correctly in my question.

In your example you said: "Put the cells or range of cells between quotes and within the quotes separated by commas" . When I do that, the function works correctly!...

...but if I select only a range (A14:A18), the command gives an error because I have to put the range between double quotes manually, you know?

I want two options in this function:
1 - Select individually, as you informed (Put the cells or range of cells between quotes and within the quotes separated by commas); or
2 - When I select only a range, I don't need to put double quotes (as it was in the original function).

Note: item 1 works ok, but in item 2 I have to put the double quotes manually for it to work.
 
Upvote 0
Try this:

VBA Code:
Public Function JOINLINES(vArray As Variant, _
                Optional lSize As Long, _
                Optional sSeparator As String = ", ") As Variant
  Dim v As Variant
  Dim lCount As Long
  Dim asOut() As String
  Dim rng As Range

  If lSize < 0 Then
    JOINLINES = CVErr(xlErrNum)
    Exit Function
  End If
  
  On Error Resume Next
    Set rng = vArray
    If rng Is Nothing Then
      Set rng = Range(vArray)
    End If
  On Error GoTo 0
  
  For Each v In rng
    If v.EntireRow.Hidden = False Then
      If v <> "" Then
        lCount = lCount + 1
        ReDim Preserve asOut(1 To lCount)
        asOut(lCount) = Replace(Replace(v, "VCI-", "", , , vbTextCompare), "PO-", "", , , vbTextCompare)
        If lCount = lSize Then Exit For
      End If
    End If
  Next v

  JOINLINES = Join(asOut, sSeparator)
End Function

Examples:
Dante Amor
ABCD
1
2VCI-2130/172130/17, 2130/18, 2130/17, 2130/17, 2134.1/17
3VCI-2134.1/172134.2/17, 2130/18, 2134.1/18, 2134.2/18, 2134.2/18
4PO-2134.2/17
5VCI-2130/18
6VCI-2134.1/18
7PO-2134.2/18
8VCI-2130/17
9VCI-2134.1/17
10PO-2134.2/17
11VCI-2130/18
12VCI-2134.1/18
13PO-2134.2/18
14PO-2134.2/18
15VCI-2130/17
16VCI-2134.1/17
17PO-2134.2/17
18VCI-2130/18
Hoja10
Cell Formulas
RangeFormula
D2D2=JOINLINES("B2,B5,B8,B15:B18",5,", ")
D3D3=JOINLINES(B10:B18,5,", ")
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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