VBA分配单元格引用功能

时间:2011-11-13 16:14:58

标签: excel excel-vba vba

我需要一个函数来将单元格引用(通过带有CTRL + SHIFT + ENTER的数组)分配给工作表。问题是:除非引用的单元格为空,否则它会正常工作,这会给阵列的所有单元格提供#VALUE ERROR。这是我使用的功能

Function GeneratePackingList(PackageTable As Range)
Dim bRow As Byte, bCol as Byte
Dim asResult()
ReDim asResult(1 To 25, 1 To 11)

With PackageTable
    For bRow = 1 To .Rows.Count
        For bCol = 1 To 11
            Select Case bCol
                'Rearranging the columns order
                Case 1 To 6:    Set asResult(bRow, bCol) = .Cells(bRow, bCol + 1)
                Case 7:         Set asResult(bRow, bCol) = .Cells(bRow, 1)
                Case 8 To 11:   Set asResult(bRow, bCol) = .Cells(bRow, bCol)
            End Select
        Next bCol
    Next bRow
End With
GeneratePackingList = asResult
End Function

所以基本上如果其中一个来自PackageTable的单元格为空,我会得到#VALUE错误并且全部丢失。我应该更改变量声明(变体,范围或其他??)是否有任何其他更有效的方式通过函数分配单元格引用?

1 个答案:

答案 0 :(得分:2)

在您的案例子句中使用Set会导致arResult成为Range的数组。这不是必要的,并且会导致您的问题。只需删除Set s

即可
            Select Case bCol
                'Rearranging the columns order
                Case 1 To 6:     asResult(bRow, bCol) = .Cells(bRow, bCol + 1)
                Case 7:          asResult(bRow, bCol) = .Cells(bRow, 1)
                Case 8 To 11:    asResult(bRow, bCol) = .Cells(bRow, bCol)
            End Select