Concatenate multiple ranges using vba

前端 未结 9 1309
臣服心动
臣服心动 2020-12-03 03:51

I have a number of ranges to concatenate independently and put the values of the concatenated ranges into different cells.

I want to:
concatenate values in Range

9条回答
  •  星月不相逢
    2020-12-03 04:49

    Function ConcatenateRange to concatenate all cells in range if they are not empty and empty "" string.

      Function ConcatenateRange(cellRange As Range, Optional Delimiter As String) As String
        Dim cel As Range, conStr As String
    
        conStr = ""
        If Delimiter <> "" Then
          For Each cel In cellRange
            If VarType(cel) <> vbEmpty And Trim(cel) <> "" Then conStr = conStr & cel & Delimiter
          Next
          ConcatenateRange = Left(conStr, Len(conStr) - Len(Delimiter))
        Else
          For Each cel In cellRange
            If VarType(cel) <> vbEmpty And Trim(cel) <> "" Then conStr = conStr & cel
          Next
          ConcatenateRange = conStr
        End If
    End Function
    

提交回复
热议问题