具有动态范围

时间:2015-06-03 08:09:17

标签: excel range

我想要一个具有动态范围的CONCATENATE函数。范围可能非常不同,请参见下面的示例。是否可以通过简单的代码来实现这一目标?

example

范围仅适用于行,其中列中的非空单元格数不同。我也需要对每两个单元格进行配对,例如。

1 个答案:

答案 0 :(得分:0)

试试这个 - 您可能需要将C1更改为数据的开头

Sub test()
i = 0
i2 = 0
With ActiveSheet
    For Each cell In .Range("C1:" & .Range("C1").End(xlDown).Address)
        i2 = 0
        temp = ""
        For Each c In .Range(.Cells(cell.Row, 3), .Cells(cell.Row, 3).End(xlToRight).Address)
            If i = 0 Then
                i = 1
            Else 'every other column
                If i2 = 0 Then'first for each row needs no extra comma
                    i = 0
                    temp = "(" & .Cells(cell.Row, -1 + c.Column).Value & "," & .Cells(cell.Row, 1 + c.Column).Value & ")"
                Else
                    i = 0
                    temp = temp & ",(" & .Cells(cell.Row, -1 + c.Column).Value & "," & .Cells(cell.Row, c.Column).Value & ")"
                End If
                i2 = i2 + 1
            End If
        Next
        .Cells(cell.Row, 1).Value = temp 'put string into column A
    Next
End With
End Sub