2

I have 2 columns each with a varying amount of rows. In this instance I want to merge (scalar multiple? multiple matrices?) the item in A1 with the range(B1:B50) and then repeat for each item in column A.

Ideally the two values would be separated by a comma. Here's what I'd like to accomplish.

What would be the best route to go? Can the matrix function work for combining text?

4

2 回答 2

1

试试这个代码:

Sub sample()

    Dim lastRowA As Long, lastRowB As Long, row As Long

    lastRowA = Range("A" & Rows.Count).End(xlUp).row
    lastRowB = Range("B" & Rows.Count).End(xlUp).row

    row = 1
    For i = 1 To lastRowA
        For j = 1 To lastRowB
            Cells(row, 4) = Cells(i, 1)
            Cells(row, 5) = Cells(i, 1) & "," & Cells(j, 2)
            row = row + 1
        Next
    Next

End Sub
于 2013-05-09T23:12:35.253 回答
0

如果您从 SQL 获取数据,则 aCROSS JOIN会为您提供两组数据的笛卡尔积。

然后你可以连接在一起。

这里使用 SQL-Server 是一个示例:

CREATE TABLE #x (columnX CHAR(1));
INSERT INTO #x
    values
    ('a'),
    ('b'),
    ('c');

CREATE TABLE #y (columnY CHAR(1));
INSERT INTO #y
    values
    ('d'),
    ('e'),
    ('f');


SELECT  #x.columnX,
    #y.columnY,
    #x.columnX + ', ' +  #y.columnY
FROM    #x
    CROSS JOIN #y
于 2013-05-10T16:16:41.250 回答