Transpose and Concatenate Personnel numbers in VBA

InterserveMan16

New Member
Joined
Aug 16, 2016
Messages
4
I have a requirement that involves a list of Personnel numbers copied into a spreadsheet that need to be transposed. They must be separated by a comma and concatenated into one cell so that they can be copied and pasted into a cell in a database. Unfortunately, this list could be just a handful of Personnel numbers, or hundreds. Though this can be achieved with “Transpose” and “Concatenate”, the users are limited in their use of Excel.</SPAN>
</SPAN>
I have searched the board threads for help and am aware of the concatenate limit in VBA, and though I can stitch code together in a clunky fashion I would really appreciate some helpful direction, particularly with the syntax.</SPAN>
</SPAN>
This is what I am trying to achieve:
</SPAN></SPAN>
Personnel number

P0000123 P0000123, P0000124, P0000125, P0000126, P0000127</SPAN></SPAN>
P0000124</SPAN></SPAN>
P0000125</SPAN></SPAN>
P0000126</SPAN></SPAN>
P0000127</SPAN></SPAN>
</SPAN></SPAN>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi Muhammad,

Thanks for your speedy reply. Your solution is great for a handful of Personnel numbers, however when a department is involved this can be many hundreds of Personnel numbers which would be tiresome and possibly prone to error.
 
Upvote 0
so I threw this together.

Code:
Option Explicit

Sub ConcatPersonnel()

    Dim i As Integer
    Dim n As Integer
    Dim lastRow As Integer
    Dim strValues As String
    
    'assuming data is listed in column A, will return concatenated in Row 1 of columns B onward.
    'Breaks down based on character limit of cells in excel.
    
    n = 2 'concats to "B1" cells(1,2)
    
    With ThisWorkbook.Sheets("Sheet1") 'Use correct sheet
    
        lastRow = .Range("A5000").End(xlUp).Row 'assuming less than 5000 records.
        
        For i = 1 To lastRow
             
            strValues = strValues & .Cells(i, 1).Value & ","
            
            If Len(strValues) > 32767 Then 'cell character limit
            
                strValues = Left(strValues, InStrRev(strValues, ",", Len(strValues) - 1) - 1) 'removes last entry and comma
                
                i = i - 1 'need to redo that entry. back we go.
                
                .Cells(1, n).Value = strValues
                
                strValues = "" 'resets for count
                
                n = n + 1 'column number to paste to
                
            End If
            
        Next i
        
        strValues = Left(strValues, Len(strValues) - 1) 'chops final comma
        .Cells(1, n).Value = strValues
        
    End With
    
End Sub

it breaks down into multiple cells if the string breaches the cell character limit.

hopefully helpful! if nothing else its a good starting point. :)
 
Last edited:
Upvote 0
Try this UDF:

Code:
Function Merge(rng As Range) As String
    Dim ar As Variant
    ar = Application.Transpose(rng)
    Merge = Join(ar, ", ")
End Function
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,500
Members
449,090
Latest member
RandomExceller01

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