Skip to main content

Concatenating Rows (JoinR) and Columns (JoinC)














Hi one and all,
Sometimes it becomes difficult to concatenate many results in an array. For Example, we use 
=Index( Table, Small( if( Array = Criteria, Row( Array ) ), Nth Small ), Column No.) 
constructions, we often want all values in the resultant array to be in one cell delimited by "," or any other delimiter. But we are unable to do this, because there is no such concatenation function provided by the Microsoft. So I have come up with a UDF that will do the needful.

Here is the syntax.

=JoinR( Range_OR_Array, Delimiter As String )
=JoinC( Range_OR_Array, Delimiter As String )



JoinR:-
       
Function JoinR(ByRef x As Variant, ByRef Delim As String) As String
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '           Developed by Vikas Gautam                               '
    '         Forum Expert at ExcelForum.Com                            '
    'For Concatenating Arrays or Ranges having One Column and Many rows '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    dLen = Len(Delim)
    With Application
        SourceArray = .Transpose(x)
        Delim2 = Delim & Delim
        Temp = Replace(Join(SourceArray, Delim), Delim2, "")
        
        Do While InStr(1, Temp, Delim2, 1) > 0
            Temp = Replace(Temp, Delim2, "")
        Loop
    End With
    
    If Left(Temp, dLen) = Delim Then Temp = Mid(Temp, dLen + 1, Len(Temp))
    If Right(Temp, dLen) = Delim Then Temp = Left(Temp, Len(Temp) - dLen)
    JoinR = Temp
    
End Function

JoinC:-
       
Function JoinC(ByRef x As Variant, ByRef Delim As String) As String
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '           Developed by Vikas Gautam                               '
    '         Forum Expert at ExcelForum.Com                            '
    'For Concatenating Arrays or Ranges having One Row and Many Columns '
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    dLen = Len(Delim)
    With Application
        SourceArray = .Transpose(.Transpose(x))
        Delim2 = Delim & Delim
        Temp = Replace(Join(SourceArray, Delim), Delim2, "")
        
        Do While InStr(1, Temp, Delim2, 1) > 0
            Temp = Replace(Temp, Delim2, "")
        Loop
    End With
    
    If Left(Temp, dLen) = Delim Then Temp = Mid(Temp, dLen + 1, Len(Temp))
    If Right(Temp, dLen) = Delim Then Temp = Left(Temp, Len(Temp) - dLen)
    JoinC = Temp

End Function

Note:- Code has been revised for more efficiency.

You can see some application in the Post #8 of the following link:-
http://www.excelforum.com/excel-programming-vba-macros/1064234-multi-dimensional-concatif-coding.html

Enjoy the codes, Good Luck.!
Attached is the example workbook.

Comments

Post a Comment

Popular posts from this blog

Automation:- Sending Invitation to Meeting Using Excel VBA

Hello Everyone, In one of the previous post, I wrote about automating Sending Emails using Excel VBA. This time I have come up with a pretty similar code. The code below sends Outlook Meeting Invitations to recipients on one click. here is the Code:- Sub Send_Invite_Auto() Dim olApp As Outlook.Application Dim olApt As AppointmentItem Set olApp = New Outlook.Application 'Creating Outlook Session Set olApt = olApp.CreateItem(olAppointmentItem) 'Creating an Appointment With olApt .Subject = "Enter the subject here." 'Subject .Start = DateAdd("d", 5, Now) 'Enter Date + Time here. .Recipients.Add ("example@gmail.com") 'Recipient Name, Alias, or any other Attribute. .MeetingStatus = olMeeting 'olAppointmentItem with Meeting status olMeeting 'becom...

Making Password Protected PDFs using Excel Vba and PDFtk Tool

Hi Everyone, This time, I have come up a VBA Code to generate Password protected PDFs using Excel. Actually, Excel Vba has .ExportAsFixedFormat Method to generate PDFs but this hasn't any Parameter which takes password to protect the PDFs. So I have used PDFtk Tool which provide Command Line Interface to make PDFs protected using Password. Actually, You can do various things using PDFtk Tool command line varying from creating, merging, Protecting and many other. So Download the PDFtk Tool from the following link:- Download PDFtk Tookit Here are the Steps:- 1. Install the PDFtk Toolkit. 2. Use the following code to Print or Export the Activesheet with a password.

Targeting Sendkeys to a Particular Window using Excel VBA

Hi Everyone, Sometimes while doing some VBA stuff, we want to target Sendkeys to a particular window which may or may not be active or present. Here is the code which will first check if the Target window is available by trying to activate it by AppActivate and then Sendkeys as desired. If Target window isn't available then you may wait until it shows up or show an error message. So Here are the code:- 1. For waiting until Windows shows up:- Sub Target_Sendkeys() On Error Resume Next 'Resuming next Statement on Error(5) Do Err.Clear 'Reseting the Err AppActivate "Blogger" 'Put the name of title bar of Target window here. DoEvents 'Avoidings hangs Loop While Err.Number = 5 SendKeys "%{ }r", True 'Restoring the window Application.Wait DateAdd("s", 1, Now) '...