Concatenate multiple ranges using vba

前端 未结 9 1268
臣服心动
臣服心动 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:27

    @Issun's solution doesn't accept output from a worksheet array formula as the argument for the 'cell_range' parameter. But a slight modification to @Issun's code fixes this. I also added a check that ignores each cell whose value is FALSE.

    Function ConcatenateRange( _
            ByVal cellArray As Variant, _
            Optional ByVal seperator As String _
                ) As String
    
        Dim cell As Range
        Dim newString As String
        Dim i As Long, j As Long
    
        For i = 1 To UBound(cellArray, 1)
            For j = 1 To UBound(cellArray, 2)
                If Len(cellArray(i, j)) <> 0 Then
                    If (cellArray(i, j) <> False) Then
                        newString = newString & (seperator & cellArray(i, j))
                    End If
                End If
            Next
        Next
    
        If Len(newString) <> 0 Then
            newString = Right$(newString, (Len(newString) - Len(seperator)))
        End If
    
        ConcatenateRange = newString
    
    End Function
    

    For example:

    A       B       (<COL vROW)
    ------  ------  -----------------
    one     1         3
    two     1         4
    three   2         5
    four    2         6
    

    Enter into cell C1 the formula below and press CTRL+ENTER to store the formula as an array formula:

    {=ConcatenateRange(IF(B3:B6=1,A3:A6),CHAR(10))}
    
    0 讨论(0)
  • 2020-12-03 04:30

    Right before Next m insert simple statement: x="" – KazimierzJawor Apr 8 '13 at 20:43

    took me several minutes to notice this answer was under comments :p

    0 讨论(0)
  • 2020-12-03 04:32

    ... I would do this very differently... Why not create a function along the lines of:

    Function ConcatMe(Rng As Range) As String
    
    Dim cl As Range
    
       ConcatMe = ""
    
       For Each cl In Rng
          ConcatMe = ConcatMe & cl.Text
       Next cl
    
    End Function
    

    And then just, for example, set F1 = ConcatMe(A1:A10) or, then write code to assign the function to the cells you want...

    Or, as @KazJaw mentioned in his comment, just set x="" before re-looping.

    Hope this helps

    0 讨论(0)
  • 2020-12-03 04:34

    Here is my ConcatenateRange. It allows you to add a seperator if you please. It is optimized to handle large ranges since it works by dumping the data in a variant array and working with it within VBA.

    You would use it like this:

    =ConcatenateRange(A1:A10)
    

    The code:

    Function ConcatenateRange(ByVal cell_range As range, _
                        Optional ByVal seperator As String) As String
    
    Dim newString As String
    Dim cellArray As Variant
    Dim i As Long, j As Long
    
    cellArray = cell_range.Value
    
    For i = 1 To UBound(cellArray, 1)
        For j = 1 To UBound(cellArray, 2)
            If Len(cellArray(i, j)) <> 0 Then
                newString = newString & (seperator & cellArray(i, j))
            End If
        Next
    Next
    
    If Len(newString) <> 0 Then
        newString = Right$(newString, (Len(newString) - Len(seperator)))
    End If
    
    ConcatenateRange = newString
    
    End Function
    
    0 讨论(0)
  • 2020-12-03 04:41

    Its very simple brother, Look out of the Excel. No need for all cumbersome formula or VBA.

    Just copy all the cells that you need to concatenate and paste it in the notepad. Now just select the space between the lines/columns (it's a TAB space actually) and find and replace it.. Done.. All cells are concatenated. Now just copy and paste it in the column and just verify.. Thats it :) Enjoy.

    I suggest you to use Notepad++ for this :) Koodos

    Vimarsh Ph. D. Plant Biotech. /

    0 讨论(0)
  • 2020-12-03 04:44

    I was looking further to see if there is a better way of writing concatenate function and found this. It seems that we all have the same working principle for the function. So its ok.

    But my function is different that it can take multiple parameters, in combination of ranges, texts and numbers.

    I assume that a delimiter is mandatory, so if i don't need it i just put "" as the last parameter).

    I also assume that blank cells are not to be skipped. That's the reason why i want the function to take multiple parameters, so i can easily omit those that that i don't want in the concatenation.

    Example of use:

    =JoinText(A1:D2,F1:I2,K1:L1,";")

    You can also use together text and number among the parameters:

    =JoinText(A1:D2,123,F1:I2,K1:L1,"PQR",";")

    I'd love to hear any comments or suggestions where it can be improved.

    Here is the code.

    Public Function JoinText(ParamArray Parameters() As Variant) As String
        Dim p As Integer, c As Integer, Delim As String
    
        Delim = Parameters(UBound(Parameters))
    
        For p = 0 To UBound(Parameters) - 1
            If TypeName(Parameters(p)) = "Range" Then
                For c = 1 To Parameters(p).Count
                    JoinText = JoinText & Delim & Parameters(p)(c)
                Next c
            Else
                JoinText = JoinText & Delim & Parameters(p)
            End If
        Next p
    
        JoinText = Replace(JoinText, Delim, "", , 1, vbBinaryCompare)
    
    End Function
    
    0 讨论(0)
提交回复
热议问题