+ Reply to Thread
Results 1 to 10 of 10

Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Hi,

    Need some help, might someone tell me a snipped to add to the code?

    Im trying to concatenate 5 cells from Range R22:V22 to Single cell DU22 but adding a dash between.

    The VBA code it does the job as long the string are single number with front zero, now i need to do the same but this time there are five cells and each one include 3 digits, Example : 000 001 010 121 110

    When are three positive digits is not a problem, but when such digits Include leading zero it wont concatenate correct.
    When I place the Formula - Concat(R22:V22) in Cell DU22 and DU23 as well, it won't display the leading zeroes at front or if the string is triple zero it just display single zero.

    Example:
    --->>>C O L U M N S
    ______R___S___T____U___V__
    Row
    22___010_000_010_112_212 -> DU22 ->Display --> 10-0-10-112-212
    23___000_120_100_120_121 -> DU23 ->Display --> 0-120-100-120-121

    This is the code I'm Using:

    Public Function ConcatAll(ByVal varData As Variant, Optional ByVal sDelimiter As String = vbNullString) As String
    'Created by TigerAvatar at www.excelforum.com, September 2012
    'Purpose is to concatenate many strings into a single string
    'Can be used with arrays, range objects, and collections
        
        Dim DataIndex As Variant    'Used to loop through arrays, range objects, and collections
        Dim strResult As String     'Used to build the result string
        
        'Test if varData is an Array, Range, or Collection
        If IsArray(varData) _
        Or TypeName(varData) = "Range" _
        Or TypeName(varData) = "Collection" Then
            
            'Found to be an, array, range object, or collection
            'Loop through each item in varData
            For Each DataIndex In varData
                'Check if the item isn't empty and if so add it to the result with the delimiter
                If Len(DataIndex) > 0 Then strResult = strResult & sDelimiter & DataIndex
            Next DataIndex
            
            'Correct strResult to remove beginning delimiter
            strResult = Mid(strResult, Len(sDelimiter) + 2)
            
        Else
            'Found not to be an array, range object, or collection
            'Simply set the result = varData
            strResult = varData
        End If
        
        'Output result
        ConcatAll = strResult
        
    End Function


    Thanks !!!

  2. #2
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Try this change

    Public Function ConcatAll(ByVal varData As Variant, Optional ByVal sDelimiter As String = vbNullString) As String
    'Created by TigerAvatar at www.excelforum.com, September 2012
    'Purpose is to concatenate many strings into a single string
    'Can be used with arrays, range objects, and collections
        
        Dim DataIndex As Variant    'Used to loop through arrays, range objects, and collections
        Dim strResult As String     'Used to build the result string
        
        'Test if varData is an Array, Range, or Collection
        If IsArray(varData) _
        Or TypeName(varData) = "Range" _
        Or TypeName(varData) = "Collection" Then
            
            'Found to be an, array, range object, or collection
            'Loop through each item in varData
            For Each DataIndex In varData
                'Check if the item isn't empty and if so add it to the result with the delimiter
                
                If Len(DataIndex) > 0 Then strResult = strResult & "-" & WorksheetFunction.Text(DataIndex, "000")
            Next DataIndex
            
            'Correct strResult to remove beginning delimiter
            strResult = Mid(strResult, Len(sDelimiter) + 2)
            
        Else
            'Found not to be an array, range object, or collection
            'Simply set the result = varData
            strResult = varData
        End If
        
        'Output result
        ConcatAll = strResult
        
    End Function
    Happy with my advice? Click on the * reputation button below

  3. #3
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Hi Crooza,

    No is displaying the same ...

    Example:
    --->>>C O L U M N S
    ______R___S___T____U___V__
    Row
    22___010_000_010_112_212 -> DU22 ->Display --> 10-0-10-112-212



  4. #4
    Forum Expert Crooza's Avatar
    Join Date
    10-19-2013
    Location
    Hunter Valley, Australia
    MS-Off Ver
    Excel 2003 /7/10
    Posts
    2,082

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Quote Originally Posted by AndyJr View Post
    Hi Crooza,

    No is displaying the same ...
    (
    Not on my example

    see attached
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Hi,

    Yes is working!!

    the Call function was ConCatAll=

    Excel cell had ConCatal (from previous code tested)

    Thank you all !!

    Crooza, Yujin, Jolivanes and sktneer !!

    Regards

  6. #6
    Forum Contributor
    Join Date
    01-03-2018
    Location
    Fukuoka, Japan
    MS-Off Ver
    Excel 2013
    Posts
    123

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    If you don't want front zeroes to vanish, you need to format the digits with Format function.

    If Len(DataIndex) > 0 Then strResult = strResult & sDelimiter & Format(DataIndex, "000")

  7. #7
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Hi Yujin,

    Is the same output my friend..

    Example:
    --->>>C O L U M N S
    ______R___S___T____U___V__
    Row
    22___010_000_010_112_212 -> DU22 ->Display --> 10-0-10-112-212



  8. #8
    Registered User
    Join Date
    10-14-2017
    Location
    Miami, Florida
    MS-Off Ver
    2016
    Posts
    83

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Hi Jujin,

    Contrary, I need the Front zeros and or all zeros (if in cell contents)

    the first thing i did was the formatting 000-000-000-000-000

  9. #9
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,692

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    Try this
    Sub AAAAA()
    Dim a As String, i As Long, j As Long
        For i = 1 To Cells(Rows.Count, 18).End(xlUp).Row
            a = CStr(Cells(i, 18))
                For j = 19 To 22
                    a = a & "-" & CStr(Cells(i, j))
                Next j
            Cells(Rows.Count, 125).End(xlUp).Offset(1) = a
        Next i
    End Sub

  10. #10
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: Concatenate 5 cells into a single But including front zeroes or all zeros, e.i 000

    You may try something like this and see if that produces the desired output.

    If IsArray(varData) _
    Or TypeName(varData) = "Range" _
    Or TypeName(varData) = "Collection" Then
        
        'Found to be an, array, range object, or collection
        'Loop through each item in varData
        For Each DataIndex In varData
            'Check if the item isn't empty and if so add it to the result with the delimiter
            If Len(DataIndex) > 0 Then
                If strResult = "" Then
                    strResult = DataIndex
                Else
                    strResult = strResult & sDelimiter & DataIndex
                End If
            End If
        Next DataIndex
        
    Else
        'Found not to be an array, range object, or collection
        'Simply set the result = varData
        strResult = varData
    End If
    
    'Output result
    ConcatAll = strResult
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Concatenate not including all Zeros in 4digit custom format
    By DoraExplorExcel in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-17-2017, 01:32 PM
  2. [SOLVED] Averages of nonconsecutive cells including zeros.
    By per11 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 08-23-2016, 02:39 PM
  3. Replies: 6
    Last Post: 03-17-2016, 12:52 PM
  4. calculating average in cells, not including zeros
    By kingy in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-23-2014, 03:00 AM
  5. [SOLVED] Adding a specified amount of zeroes in front of a number
    By ShareTheGlobe in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-31-2014, 06:20 AM
  6. Replies: 5
    Last Post: 04-09-2012, 02:28 PM
  7. [SOLVED] insert zeros in front of cells already filled with data.
    By Horizon in forum Excel General
    Replies: 2
    Last Post: 01-16-2006, 04:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1