|
楼主 |
发表于 2024-3-3 16:14
|
显示全部楼层
Sub CombineData()
Dim ar As Range, br As Range, cr() As Variant
Dim a As Long, b As Long, a1 As Long, b1 As Long
Dim i As Long, ii As Long, j As Long, r As Long
' 获取第一个数据范围
Set ar = Range("A1").Resize(ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row, 1)
' 获取第二个数据范围
Set br = Range("I1").Resize(ActiveSheet.Cells(ActiveSheet.Rows.Count, "I").End(xlUp).Row, 1)
' 获取数据范围的行数和列数
a = ar.Rows.Count: b = br.Rows.Count
a1 = ar.Columns.Count: b1 = br.Columns.Count
' 初始化结果数组
ReDim cr(1 To a * b, 1 To a1 + b1)
' 将第一个数据范围的数据复制到结果数组
For i = 1 To a
For j = 1 To a1
cr(r + 1, j) = ar(i, j)
Next j
' 将第二个数据范围的数据复制到结果数组
For ii = 1 To b
For j = a1 + 1 To a1 + b1
cr(r + 1, j) = br(ii, j - a1)
Next j
r = r + 1
Next ii
Next i
' 将结果数组输出到指定位置
Range("M1").Resize(UBound(cr, 1), UBound(cr, 2)).Value = cr
End Sub
这样写,怎么完善? |
|