Arrays 将2个范围合并为一个数组并进行筛选
我有两个范围,都有相同的行数,不同的列数(可以不同) 范围1:Arrays 将2个范围合并为一个数组并进行筛选,arrays,excel,vba,filter,Arrays,Excel,Vba,Filter,我有两个范围,都有相同的行数,不同的列数(可以不同) 范围1: A, 1, 1, 1 B, 2, 4, 8 C, 3, 9, 27 D, 4, 16, 64 范围2: 1, 1 16, 64 81, 256, 1024 我想将这些值导入Excel中的多数组,但前提是范围2的第n列(本例中为第2列)不是空值。最后我会有一个数组,看起来像这样: 产出1: A, 1, 1, 1, 1, 1 B
A, 1, 1, 1
B, 2, 4, 8
C, 3, 9, 27
D, 4, 16, 64
范围2:
1, 1
16, 64
81,
256, 1024
我想将这些值导入Excel中的多数组,但前提是范围2的第n列(本例中为第2列)不是空值。最后我会有一个数组,看起来像这样:
产出1:
A, 1, 1, 1, 1, 1
B, 2, 4, 8, 16, 32
D, 4, 16, 64, 256, 1024
到目前为止,我已经启动了一个功能:
Function Report(rng1 As Range, rng2 As Range)
Dim matrix() As Double
Dim all_rng As Range
all_rng = Union(rng1, rng2)
End Function
这是可能的决定。
注:
1.为了测试方便,我将
函数
更改为Sub
,因为我无法一步一步地完成函数2.有几行用于测试(在注释中标记)
3.我假设要填充的列的正确数量在其第一行
rng2
中4.有带有
的注释行。选择语句-取消注释,用F8一步一步走,您将看到它是如何工作的
答案是
我对你的矩阵是这样的:
并且从第10行开始有这样的输出:
代码如下:
Sub Report() 'rng1 As Range, rng2 As Range)
Dim matrix() As Variant ' use variant if you have a mix of letters and numbers
Dim x As Long, y As Long
Dim r As Long, c As Long
Dim rows() As Long, i As Long, rowCnt As Long
' used for test purposes
Dim rng1 As Range, rng2 As Range
Set rng1 = Range(Cells(1, 1), Cells(4, 4))
Set rng2 = Range(Cells(1, 9), Cells(4, 10))
' find out columns count per each range's row 1
x = Range(rng1.Cells(1, 1), rng1.Cells(1, rng1.Columns.Count)).Columns.Count
' I assume that the correct number of columns in rng2 is in the first row
' you may change the row number if needed
y = Range(rng2.Cells(1, 1), rng2.Cells(1, rng2.Columns.Count)).Columns.Count
' check that all rows have all columns filled
For i = 0 To rng1.rows.Count - 1
' if all columns in rng2 are filled then add number of the row to an array of row numbers
If Not rng2.Cells(i + 1, y) = " " Then ' fix evaluation condition if needed - that is what was copied from post
ReDim Preserve rows(rowCnt)
rows(rowCnt) = i + 1
rowCnt = rowCnt + 1
End If
Next
i = UBound(rows) - 1
' set dimension of an matrix array
ReDim matrix(rows(i), x + y)
' start filling the matrix
' go through matrix by row
For r = LBound(rows) To UBound(rows)
' fill each row column by column
'gothrough first range - it has x columns in it
For c = 0 To x - 1
' rng1.Cells(rows(r), c + 1).Select
matrix(r, c) = rng1.Cells(rows(r), c + 1).Value
Next
' then without changing matrix's row
' go through second range - it has y columns
For c = 0 To y - 1
' rng2.Cells(rows(r), c + 1).Select
matrix(r, c + rows(UBound(rows))) = rng2.Cells(rows(r), c + 1).Value
Next
Next
' print the matrix to the sheet (optional - delete when convert this back to Function)
For r = LBound(matrix) To UBound(matrix)
For c = 0 To x + y - 1
Cells(10 + r, c + 1) = matrix(r, c)
Next
Next
End Sub
如果您有任何问题,请发表评论。阵列备选方案
为了演示一种结构清晰的方法,使用数组而不是在每个单元格中循环:
将数据分配给数组1
和2
将第二个数组的列值添加到重新标注尺寸的数组中
通过删除空行(检查数组中的第n列two
)-通过单个代码行(使用helper函数)重新构造生成的数组
将结果写入任何目标范围-通过单个代码行
辅助函数getRowNo()
Function getRowNo(arr) As Variant()
' Note: receives last column values of array two as 1-dim 1based array
' Purp: returns 2-dim 1-based array with non-empty row numbers of array two
Dim i As Long, ii As Long, tmp()
ReDim tmp(1 To 1, 1 To UBound(arr)) ' provide for temporary array
For i = LBound(arr) To UBound(arr)
If Len(arr(i) & "") Then ' omit empty item
ii = ii + 1 ' increment temp counter
tmp(1, ii) = i ' enter row number of original column data
End If
Next i
ReDim Preserve tmp(1 To 1, 1 To ii) ' correct last dimension
getRowno = Application.Transpose(tmp) ' return 2-dim array with rownumbers to be preserved
End Function
您想要一个二维值数组还是一个包含其他一维值数组的一维数组?一个二维数组,其第二个值范围刚刚附加到结束列。您只需使用x=rng1.columns.count和y=rng2.columns.count,不需要假设。抱歉-我确实有一个问题-当我将其转换为函数时,我在ReDim Preserve rows(rowCnt)
什么类型的错误?(回家后会回答)抱歉-我修复了那个错误-这是由于我的Dim
出错造成的。我还没有宣布。现在一切都正常了-你的代码太棒了,谢谢。你是我今天遇到的世界上最好的人:D
Function getRowNo(arr) As Variant()
' Note: receives last column values of array two as 1-dim 1based array
' Purp: returns 2-dim 1-based array with non-empty row numbers of array two
Dim i As Long, ii As Long, tmp()
ReDim tmp(1 To 1, 1 To UBound(arr)) ' provide for temporary array
For i = LBound(arr) To UBound(arr)
If Len(arr(i) & "") Then ' omit empty item
ii = ii + 1 ' increment temp counter
tmp(1, ii) = i ' enter row number of original column data
End If
Next i
ReDim Preserve tmp(1 To 1, 1 To ii) ' correct last dimension
getRowno = Application.Transpose(tmp) ' return 2-dim array with rownumbers to be preserved
End Function