我有2列,每列有不同的行数。在这个例子中,我想合并(标量多个?多个矩阵?)A1中的项目与范围(B1:B50),然后对A列中的每个项目重复。
理想情况下,这两个值将用逗号分隔。 Here's what I'd like to accomplish。
最佳路线是什么?矩阵函数可以用于组合文本吗?
答案 0 :(得分: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
答案 1 :(得分:0)
如果您从SQL获取数据,那么CROSS 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