VBA中从二维数组传递单行数组给自定义函数时仅获取单个值的问题
问题根因
VBA 原生语法规定,For Each 遍历数组时的迭代单位是数组中的单个标量元素,无论数组是多少维,都不会自动返回整行/整列的子数组,这就是循环里拿到的始终是单个单元格值、拿不到整行数组的核心原因。
实现方法
放弃For Each遍历的写法,改用行索引计数循环逐行处理,根据你是否要保留原有CheckRow的入参逻辑,二选一即可:
方案1(性能最优,推荐)
直接修改CheckRow的入参,传入当前行号和源数组,在函数内部按列索引取值判断,不需要额外构造一维数组,没有额外内存开销:
Public myRows As Variant Public myTable As ListObject Sub SendEmails() Dim rowIdx As Long SetMyTable ' 自动获取数组的行上下界,不需要硬编码行数 For rowIdx = LBound(myRows, 1) To UBound(myRows, 1) Debug.Print CheckRow(rowIdx, myRows) Next rowIdx End Sub Function CheckRow(rowIdx As Long, sourceArr As Variant) As Boolean Dim IsRowValid As Boolean IsRowValid = True ' 按行号+列号直接取值判断 If IsEmpty(sourceArr(rowIdx, 1)) Then IsRowValid = False If IsEmpty(sourceArr(rowIdx, 2)) Then IsRowValid = False If IsEmpty(sourceArr(rowIdx, 3)) Then IsRowValid = False If IsEmpty(sourceArr(rowIdx, 4)) Then IsRowValid = False If IsEmpty(sourceArr(rowIdx, 5)) Then IsRowValid = False CheckRow = IsRowValid End Function
方案2(兼容原有CheckRow逻辑)
如果你不想修改现有CheckRow的入参和判断逻辑,可以在每次行循环时,把当前行的所有列值读取到一个临时一维数组中,再传入函数:
Public myRows As Variant Public myTable As ListObject Sub SendEmails() Dim rowIdx As Long, colIdx As Long Dim currentRow As Variant SetMyTable ' 初始化行数组的长度和源数组的列数一致 ReDim currentRow(LBound(myRows, 2) To UBound(myRows, 2)) For rowIdx = LBound(myRows, 1) To UBound(myRows, 1) ' 填充当前行的所有列值到一维数组 For colIdx = LBound(myRows, 2) To UBound(myRows, 2) currentRow(colIdx) = myRows(rowIdx, colIdx) Next colIdx Debug.Print CheckRow(currentRow) Next rowIdx End Sub ' 原有CheckRow代码无需任何修改 Function CheckRow(Row As Variant) As Boolean Dim IsRowValid As Boolean IsRowValid = True If IsEmpty(Row(1)) = True Then IsRowValid = False End If If IsEmpty(Row(2)) = True Then IsRowValid = False End If If IsEmpty(Row(3)) = True Then IsRowValid = False End If If IsEmpty(Row(4)) = True Then IsRowValid = False End If If IsEmpty(Row(5)) = True Then IsRowValid = False End If CheckRow = IsRowValid End Function
提示:用
LBound(数组, 维度)、UBound(数组, 维度)获取数组的上下界,比硬编码1到33、1到9的兼容性更好,后续数据源的行数、列数变化时不需要修改循环代码。
内容的提问来源于stack exchange,提问作者Dobi Tamás
相关产品推荐
相关产品推荐

