Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate value in each column based on cell value

Hello I need to concatenate the columns depending on whether the rows are null or have any value.
I.e. I have data like this

enter image description here

And I need get data like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

The problem is that the columns I want to join will be more than 50 and for each I have to check if there is a blank or a number in that column and row.

Can you help me with this please?

>Solution :

If you asked for a VBA solution, please try the next code. It will process as many Value columns will be, and returns the processing result two columns after the last one. Using arrays, the code will be very fast even for a large range:

Sub ConcatenateWithValues()
   Dim sh As Worksheet, lastR As Long, lastCol As Long, arr, arrH, arrFin
   Dim strConc As String, i As Long, j As Long
   
   Set sh = ActiveSheet
   lastR = sh.Range("A" & sh.Rows.count).End(xlUp).row
   lastCol = sh.cells(1, sh.Columns.count).End(xlToLeft).Column
   
   arr = sh.Range("A1", sh.cells(lastR, lastCol)).Value
   arrH = sh.Range("A1", sh.cells(1, lastCol)).Value
   ReDim arrFin(1 To UBound(arr), 1 To 2)
   arrFin(1, 1) = arrH(1, 1): arrFin(1, 2) = "Concatenate_Value"
   For i = 2 To UBound(arr)
        For j = 2 To lastCol
            If arr(i, j) <> "" Then strConc = strConc & arrH(1, j) & ":" & arr(i, j) & ";"
        Next
        arrFin(i, 1) = arr(i, 1): arrFin(i, 2) = left(strConc, Len(strConc) - 1)
        strConc = ""
   Next i
   'Drop the processed array content at once:
   sh.cells(1, lastCol + 2).Resize(UBound(arrFin), 2).Value = arrFin
End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading