Arrays 如何在VBA中引用变量矩阵中的整列或整行?

Arrays 如何在VBA中引用变量矩阵中的整列或整行?,arrays,excel,vba,matrix,reference,Arrays,Excel,Vba,Matrix,Reference,我花了很多时间寻找一种方法,如何引用我在VBA中创建的矩阵中的范围(列或行) 一个简单的例子是循环4×x矩阵的第4行,对x列的第1列到第3列求和 下面的代码是实现这一目标的漫长道路: x = 10 Dim Matrix() As Variant ReDim Matrix(1 to 4, 1 to x) For c = 1 to x Matrix(4, c) = Application.WorksheetFunction.Sum(Matrix(1, c), Matrix(2, c),

我花了很多时间寻找一种方法,如何引用我在VBA中创建的矩阵中的范围(列或行)

一个简单的例子是循环4×x矩阵的第4行,对x列的第1列到第3列求和

下面的代码是实现这一目标的漫长道路:

x = 10

Dim Matrix() As Variant
ReDim Matrix(1 to 4, 1 to x)

For c = 1 to x

   Matrix(4, c) = Application.WorksheetFunction.Sum(Matrix(1, c), Matrix(2, c), Matrix(3, c))

Next
我正在寻找一种方法,这样做不会出错:

For c = 1 to x

   Matrix(4, c) = Application.WorksheetFunction.Sum(Range(Matrix(1, c), Matrix(3, c)))

Next
当我尝试运行它时,会弹出以下错误:

*运行时错误“1004”:
对象“\u Global”的方法“range”失败*

你能帮我解决这个问题吗,因为其他的解决方法似乎相当耗时

谢谢


Andy

正如James Poag所说,范围不是一个全局函数,必须与工作表一起调用。如果要使用
工作表函数.Sum
,则必须将矩阵复制到工作表中。在以下示例中:

  • 创建一个4 x 10的随机数矩阵
  • 创建新的工作表
  • 将矩阵复制到工作表中
  • 使用
    工作表函数.Sum
    添加范围内的行,然后
  • 删除VBA宏添加的工作表

    Option Explicit
    
    Public Sub Matrixer()
        Dim x As Long
        x = 10
        Dim matrix() As Double
        ReDim matrix(1 To 4, 1 To x)
    
        'Generate the matrix
        Dim rowNDX As Long
        Dim colNDX As Long
        For rowNDX = 1 To UBound(matrix, 1)
            For colNDX = 1 To UBound(matrix, 2)
                Randomize
                matrix(rowNDX, colNDX) = Rnd
            Next colNDX
        Next rowNDX
    
        'Write the maxtrix to a sheet
        'First add a worksheet to do the calculation
        Dim wb As Workbook: Set wb = ActiveWorkbook
        Dim strName As String: strName = "MATRIXCALC"
    
        Dim ws As Worksheet
        Set ws = wb.Worksheets.Add(Type:=xlWorksheet)
        With ws
            .Name = strName
        End With
    
        'Write the maxtrix to the sheet
        'This code was provide/adapted from Chip Pearson's blog at
        'http://www.cpearson.com/excel/ArraysAndRanges.aspx
        Dim Destination As Range
        Set Destination = ws.Range("A1")
        Destination.Resize(UBound(matrix, 1), UBound(matrix, 2)).Value = matrix
    
        'Use the worksheet function to Sum the range
        Dim RowSum(4) As Double
        Dim rngSum As Range
        For rowNDX = 1 To 4
            Set rngSum = ws.Range("A" & Trim(CStr(rowNDX)) & ":A" & Trim(CStr(UBound(matrix, 2))))
            RowSum(rowNDX) = WorksheetFunction.Sum(rngSum)
    
        Next rowNDX
    
        'Delete the worksheet added by the macro
        'Prevent asking user if it's ok to delete worksheet
        Application.DisplayAlerts = False
        ws.Delete
        'Turn application display alerts back on.
        Application.DisplayAlerts = True
    End Sub
    

  • 范围不是全局函数,您必须在工作表上调用它或激活工作表。在Word VBA中,我还能够使用工作表(…)执行类似于
    。。。以
    结束。用wsTarget搜索我给你的
    链接,看看我的意思。也许你可以说,使用ActiveSheet时,我没有数据源,因为我使用随机生成的变量为蒙特卡罗模拟生成了一个矩阵表。我可以做的是为“矩阵”(“and”,c)”声明一个字符串变量,并在1,2,3中填充一个变量。。。。。。。不过,有没有一种方法可以在一个范围内做到这一点?为什么您更喜欢第二种方法?为什么不简单地添加值,而不是返回到工作表。函数?因为如果矩阵是50行乘50列,那么我不希望逐个添加每个需要添加的单元格,因此添加50个差分矩阵(第1、2、3、c行)。@Andy You说过“那么,我不希望逐个添加需要添加的每个单元格,因此添加50个差异矩阵(第1、2、3、c行)“嗯,没人会的,真的。您应该研究嵌套循环的概念!那么,即使是一个100000 x 100000的矩阵也很容易处理,是的。谢谢杰米·里斯的帮助。然后,我假设将矩阵粘贴到excel中并使用实际的单元格引用是必须的,这样才能工作,并避免重复1000x相同的代码。谢谢,我将合并粘贴。如果有更简单的方法(比如使用范围,或者使用完整列),我将非常乐意了解这一点。非常感谢。