Advertisement
Guest User

StringConcat Function Code

a guest
Jul 8th, 2013
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function StringConcat(Sep As String, ParamArray Args()) As Variant
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. ' StringConcat
  4. ' By Chip Pearson, chip@cpearson.com, www.cpearson.com
  5. '                  www.cpearson.com/Excel/stringconcatenation.aspx
  6. ' This function concatenates all the elements in the Args array,
  7. ' delimited by the Sep character, into a single string. This function
  8. ' can be used in an array formula. There is a VBA imposed limit that
  9. ' a string in a passed in array (e.g.,  calling this function from
  10. ' an array formula in a worksheet cell) must be less than 256 characters.
  11. ' See the comments at STRING TOO LONG HANDLING for details.
  12. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  13. Dim S As String
  14. Dim N As Long
  15. Dim M As Long
  16. Dim R As Range
  17. Dim NumDims As Long
  18. Dim LB As Long
  19. Dim IsArrayAlloc As Boolean
  20.  
  21. '''''''''''''''''''''''''''''''''''''''''''
  22. ' If no parameters were passed in, return
  23. ' vbNullString.
  24. '''''''''''''''''''''''''''''''''''''''''''
  25. If UBound(Args) - LBound(Args) + 1 = 0 Then
  26.     StringConcat = vbNullString
  27.     Exit Function
  28. End If
  29.  
  30. For N = LBound(Args) To UBound(Args)
  31.     ''''''''''''''''''''''''''''''''''''''''''''''''
  32.    ' Loop through the Args
  33.    ''''''''''''''''''''''''''''''''''''''''''''''''
  34.    If IsObject(Args(N)) = True Then
  35.         '''''''''''''''''''''''''''''''''''''
  36.        ' OBJECT
  37.        ' If we have an object, ensure it
  38.        ' it a Range. The Range object
  39.        ' is the only type of object we'll
  40.        ' work with. Anything else causes
  41.        ' a #VALUE error.
  42.        ''''''''''''''''''''''''''''''''''''
  43.        If TypeOf Args(N) Is Excel.Range Then
  44.             '''''''''''''''''''''''''''''''''''''''''
  45.            ' If it is a Range, loop through the
  46.            ' cells and create append the elements
  47.            ' to the string S.
  48.            '''''''''''''''''''''''''''''''''''''''''
  49.            For Each R In Args(N).Cells
  50.                 If Len(R.Text) > 0 Then
  51.                     S = S & R.Text & Sep
  52.                 End If
  53.             Next R
  54.         Else
  55.             '''''''''''''''''''''''''''''''''
  56.            ' Unsupported object type. Return
  57.            ' a #VALUE error.
  58.            '''''''''''''''''''''''''''''''''
  59.            StringConcat = CVErr(xlErrValue)
  60.             Exit Function
  61.         End If
  62.    
  63.     ElseIf IsArray(Args(N)) = True Then
  64.         '''''''''''''''''''''''''''''''''''''
  65.        ' ARRAY
  66.        ' If Args(N) is an array, ensure it
  67.        ' is an allocated array.
  68.        '''''''''''''''''''''''''''''''''''''
  69.        IsArrayAlloc = (Not IsError(LBound(Args(N))) And _
  70.             (LBound(Args(N)) <= UBound(Args(N))))
  71.         If IsArrayAlloc = True Then
  72.             ''''''''''''''''''''''''''''''''''''
  73.            ' The array is allocated. Determine
  74.            ' the number of dimensions of the
  75.            ' array.
  76.            '''''''''''''''''''''''''''''''''''''
  77.            NumDims = 1
  78.             On Error Resume Next
  79.             Err.Clear
  80.             NumDims = 1
  81.             Do Until Err.Number <> 0
  82.                 LB = LBound(Args(N), NumDims)
  83.                 If Err.Number = 0 Then
  84.                     NumDims = NumDims + 1
  85.                 Else
  86.                     NumDims = NumDims - 1
  87.                 End If
  88.             Loop
  89.             On Error GoTo 0
  90.             Err.Clear
  91.             ''''''''''''''''''''''''''''''''''
  92.            ' The array must have either
  93.            ' one or two dimensions. Greater
  94.            ' that two caues a #VALUE error.
  95.            ''''''''''''''''''''''''''''''''''
  96.            If NumDims > 2 Then
  97.                 StringConcat = CVErr(xlErrValue)
  98.                 Exit Function
  99.             End If
  100.             If NumDims = 1 Then
  101.                 For M = LBound(Args(N)) To UBound(Args(N))
  102.                     If Args(N)(M) <> vbNullString Then
  103.                         S = S & Args(N)(M) & Sep
  104.                     End If
  105.                 Next M
  106.                
  107.             Else
  108.                 ''''''''''''''''''''''''''''''''''''''''''''''''
  109.                ' STRING TOO LONG HANDLING
  110.                ' Here, the error handler must be set to either
  111.                '   On Error GoTo ContinueLoop
  112.                '   or
  113.                '   On Error GoTo ErrH
  114.                ' If you use ErrH, then any error, including
  115.                ' a string too long error, will cause the function
  116.                ' to return #VALUE and quit. If you use ContinueLoop,
  117.                ' the problematic value is ignored and not included
  118.                ' in the result, and the result is the concatenation
  119.                ' of all non-error values in the input. This code is
  120.                ' used in the case that an input string is longer than
  121.                ' 255 characters.
  122.                ''''''''''''''''''''''''''''''''''''''''''''''''
  123.                On Error GoTo ContinueLoop
  124.                 'On Error GoTo ErrH
  125.                Err.Clear
  126.                 For M = LBound(Args(N), 1) To UBound(Args(N), 1)
  127.                     If Args(N)(M, 1) <> vbNullString Then
  128.                         S = S & Args(N)(M, 1) & Sep
  129.                     End If
  130.                 Next M
  131.                 Err.Clear
  132.                 M = LBound(Args(N), 2)
  133.                 If Err.Number = 0 Then
  134.                     For M = LBound(Args(N), 2) To UBound(Args(N), 2)
  135.                         If Args(N)(M, 2) <> vbNullString Then
  136.                             S = S & Args(N)(M, 2) & Sep
  137.                         End If
  138.                     Next M
  139.                 End If
  140.                 On Error GoTo ErrH:
  141.             End If
  142.         Else
  143.             If Args(N) <> vbNullString Then
  144.                 S = S & Args(N) & Sep
  145.             End If
  146.         End If
  147.         Else
  148.         On Error Resume Next
  149.         If Args(N) <> vbNullString Then
  150.             S = S & Args(N) & Sep
  151.         End If
  152.         On Error GoTo 0
  153.     End If
  154. ContinueLoop:
  155. Next N
  156.  
  157. '''''''''''''''''''''''''''''
  158. ' Remove the trailing Sep
  159. '''''''''''''''''''''''''''''
  160. If Len(Sep) > 0 Then
  161.     If Len(S) > 0 Then
  162.         S = Left(S, Len(S) - Len(Sep))
  163.     End If
  164. End If
  165.  
  166. StringConcat = S
  167. '''''''''''''''''''''''''''''
  168. ' Success. Get out.
  169. '''''''''''''''''''''''''''''
  170. Exit Function
  171. ErrH:
  172. '''''''''''''''''''''''''''''
  173. ' Error. Return #VALUE
  174. '''''''''''''''''''''''''''''
  175. StringConcat = CVErr(xlErrValue)
  176. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement