Concat UDF - VBA Question

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
926
Office Version
  1. 2010
Platform
  1. Windows
I wanted to ask how to change the code below so it will ignore blank cells:

The UDF works great but I receive a "value" when all the cells in a range are all blank
Code:
Function Concat(myRange As Range, Optional myDelimiter As String)
Dim r As Range

Application.Volatile
For Each r In myRange
If Len(r.Text) > 0 Then
Concat = Concat & Format(r, "000") & myDelimiter 
End If
Next r
If Len(myDelimiter) > 0 Then
Concat = Left(Concat, Len(Concat) - Len(myDelimiter))
End If
End Function


The following code works and does not result in a "value", how do I change it by adding the following line?
Code:
 Concat = Concat & Format(r, "000") & myDelimiter

Code:
Function Concat(rng As Range, Optional sep As String = ",") As String
    Dim rngCell As Range
    Dim strResult As String
    For Each rngCell In rng
        If rngCell.Value <> "" Then
            strResult = strResult & sep & rngCell.Value
        End If
    Next rngCell
    If strResult <> "" Then
        strResult = Mid(strResult, Len(sep) + 1)
    End If
    Concat = strResult
End Function

Thank you in advance!!
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I think this works.

Code:
Function Concat(myRange As Range, Optional myDelimiter As String)

Dim r As Range

Application.Volatile

    For Each r In myRange
        If Len(r.Text) > 0 And Not (IsError(r)) Then
            Concat = Concat & Format(r, "000") & myDelimiter
        End If
    Next r

    If Right(Concat, Len(myDelimiter)) = myDelimiter Then
        Concat = Left(Concat, Len(Concat) - Len(myDelimiter))
    End If

    If Concat = 0 Then
        Concat = ""
    End If
    
End Function
 
Upvote 0
IF the ranges you will be concatenating will be either single rows of contiguous cells, or single columns of contiguous cells, then you can use this non-looping function to perform your concatenations...
Code:
Function JoinString(varRange As Range, Optional varDelimiter As String = ",") As String
  With WorksheetFunction
    If varRange.Columns.Count = 1 Then
      JoinString = .Trim(Join(.Transpose(varRange.Value), varDelimiter))
    Else
      JoinString = .Trim(Join(.Index(varRange.Value, 1, 0), varDelimiter))
    End If
    JoinString = Replace(Replace(.Trim(Replace(Replace(JoinString, " ", Chr( _
                 1)), varDelimiter, " ")), " ", varDelimiter), Chr(1), " ")
  End With
End Function
 
Last edited:
Upvote 0
Thanks Rick.

Is there a way to change your code so that any numbers with a leading zero will be displayed? The range of data is all 3 digit numbers from 000 through 999. Thanks again.
 
Upvote 0
Thanks Rick.

Is there a way to change your code so that any numbers with a leading zero will be displayed? The range of data is all 3 digit numbers from 000 through 999. Thanks again.
If you had entered your values with the leading zeroes into a cell formatted as Text, or if you put an apostrophe in front of the number with leading zeroes manually typed in (to make it a Text value), then the function I posted would have worked, but if you are using Cell Formatting to display those leading zeroes, then the function I posted cannot be modified to concatenate the values using those leading zeroes (mainly because they are not really there)... you will then need to use a loop solution so that each individual value can be treated separately in accordance with what is being displayed in the cell.
 
Upvote 0
...but if you are using Cell Formatting to display those leading zeroes, then the function I posted cannot be modified to concatenate the values using those leading zeroes (mainly because they are not really there)... you will then need to use a loop solution so that each individual value can be treated separately in accordance with what is being displayed in the cell.
I found a way to write my function, still without using any loops, which will format any numbers to be three digits long with leading zeroes if necessary...
Code:
Function JoinString(Rng As Range, Optional Delimiter As String = ",") As String
  Dim EvalArg As String
  EvalArg = Replace("IF(LEN(@),IF(ISNUMBER(@),TEXT(@,""000""),@),"""")", "@", Rng.Address)
  If Rng.Columns.Count = 1 Then
    JoinString = Join(Application.Transpose(Evaluate(EvalArg)), Delimiter)
  Else
    JoinString = Join(Evaluate(EvalArg), Delimiter)
  End If
  JoinString = Replace(Replace(Application.Trim(Replace(Replace(JoinString, " ", _
                       Chr(1)), Delimiter, " ")), " ", Delimiter), Chr(1), " ")
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,203,247
Messages
6,054,374
Members
444,721
Latest member
BAFRA77

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