简体   繁体   English

VBA:将2D随机数乘以1D数组收到运行时错误

[英]VBA: Multiplying 2D arrary by 1D array receiving runtime error

I'n new to vba and trying to write some code to do some operations on an array. 我是vba的新手,正在尝试编写一些代码对数组进行一些操作。

Presently just trying to do straight forward multiplication to make sure I am writing the code correctly but unfortunately I keep receiving the following error. 目前,只是尝试进行直接乘法以确保我正确地编写了代码,但不幸的是,我一直收到以下错误。

Run time error '9': Subscript out of range

My code is as follows: 我的代码如下:

Sub ArrayOp()
Dim rng As Range
Dim rng2 As Range
Dim Destination As Range

Dim numRows As Integer
Dim numCols As Integer

Dim ArrRng As Variant
Dim ArrRng2 As Variant
Dim myarray As Variant

Set rng = Application.InputBox("Select variable data", "Obtain Range Object", Type:=8)
Set rng2 = Application.InputBox("Select residuals", "Obtain Range Object", Type:=8)
Set Destination = Application.InputBox("Select starting location for output", "Obtain Range Object", Type:=8)

ArrRng = rng
ArrRng2 = rng2

numRows = UBound(ArrRng, 1) - LBound(ArrRng, 1) + 1
numCols = UBound(ArrRng, 2) - LBound(ArrRng, 2) + 1

ReDim myarray(numRows, numCols)

For i = 1 To numRows
    For j = 1 To numCols
        myarray(i, j) = ArrRng(i, j) + ArrRng2(i)
    Next j
Next i

Destination.Resize(UBound(myarray, 1), UBound(myarray, 2)).Value = myarray 


End Sub

Unfortunately I am unable to spot the error, as the array size should be correct. 不幸的是,由于数组大小正确,我无法发现错误。

Basically user first selects 2D array eg 24 Rows, 5 Cols, then user selects 1D array of 24Rows. 基本上,用户首先选择2D数组,例如24行,5列,然后用户选择24行的1D数组。

I then want to output another array where each element of the 2D array is multiplied by the relevant element of the 1D array 然后,我想输出另一个数组,其中2D数组的每个元素都乘以1D数组的相关元素

eg 2D array all 5 columns of row 1 multipled by row 1 of 1D array etc. 例如2D数组,第1行的所有5列乘以1D数组的第1行,依此类推。

I hope the above is clear any help or pointers would be greatly appreciated. 我希望以上内容很清楚,对您的帮助或指点将不胜感激。

Thanks 谢谢

Your code had many problems. 您的代码有很多问题。 Here is a working version of matrix to vector multiplication: 这是矩阵到向量乘法的工作版本:

Sub ArrayOp()
Dim rng As Range
Dim rng2 As Range
Dim Destination As Range

Dim numRows As Integer
Dim numCols As Integer

Dim ArrRng As Variant
Dim ArrRng2 As Variant
Dim myarray As Variant

Set rng = Application.InputBox("Select variable data", "Obtain Range Object", Type:=8)
Set rng2 = Application.InputBox("Select residuals", "Obtain Range Object", Type:=8)
Set Destination = Application.InputBox("Select starting location for output", "Obtain Range Object", Type:=8)

ArrRng = rng.Value
ArrRng2 = rng2.Value
numRows = rng.Rows.Count
numCols = rng.Columns.Count
If numCols <> rng2.Rows.Count Then
    MsgBox "Inconsistent Matrix Columns with Vector Rows", vbCritical, "Multiplication"
    Exit Sub
End If
ReDim myarray(1 To numRows, 1 To 1)
Dim i As Integer, j As Integer
Dim sum As Double
For i = 1 To numRows
    sum = 0#
    For j = 1 To numCols
        sum = sum + ArrRng(i, j) * ArrRng2(j, 1)
    Next j
    myarray(i, 1) = sum
Next i
Destination.Resize(numRows, 1).Value = myarray    
End Sub

选择1选择2选择3

结果

The above can also be accomplished by 以上也可以通过

Sub ArrayOp2()
Dim rng As Range
Dim rng2 As Range
Dim Destination As Range

Set rng = Application.InputBox("Select variable data", "Obtain Range Object", Type:=8)
Set rng2 = Application.InputBox("Select residuals", "Obtain Range Object", Type:=8)
Set Destination = Application.InputBox("Select starting location for output", "Obtain Range Object", Type:=8)

Destination.Resize(rng.Rows.Count,1).Value = WorksheetFunction.MMult(rng, rng2)

End Sub

After reading your responses I went back and edited my code, which now works for the mulitplication. 阅读您的回复后,我回过头来编辑了我的代码,该代码现在可用于多重复制。

Option Base 1
Sub ArrayOp()
Dim rng As Range
Dim rng2 As Range
Dim Destination As Range

Dim numRows As Integer
Dim numCols As Integer

Dim ArrRng As Variant
Dim ArrRng2 As Variant
Dim myarray As Variant

Set rng = Application.InputBox("Select variable data", "Obtain Range Object", Type:=8)
Set rng2 = Application.InputBox("Select residuals", "Obtain Range Object", Type:=8)
Set Destination = Application.InputBox("Select starting location for output", "Obtain Range Object", Type:=8)

ArrRng = rng
ArrRng2 = rng2

numRows = rng.Rows.Count
numCols = rng.Columns.Count

ReDim myarray(numRows, numCols)

If numRows <> rng2.Rows.Count Then
    MsgBox "Please make sure the same number of observations are available for the residuals and the variables", vbCritical, "Multiplication"
    Exit Sub
End If

For i = 1 To numRows
    For j = 1 To numCols
        myarray(i, j) = ArrRng(i, j) * ArrRng2(i, 1)
    Next j
Next i

Destination.Resize(UBound(myarray, 1), UBound(myarray, 2)).Value = myarray


End Sub

This code will ultimately be edited so that I can residistribute residuals across the variables of a regression based on the percentage each variable contributes in each observation. 最终将对该代码进行编辑,以便我可以根据每个变量在每个观察值中所占的百分比在回归变量之间重新分配残差。

So now I have to figure out how to sum all columns in each row, then divide each element by this sum to get a percentage eg each row will be 100%. 因此,现在我必须弄清楚如何对每一行中的所有列求和,然后将每个元素除以该总和以获得百分比,例如,每一行将为100%。 The multiply this by the residual and add back to the variable. 将此乘以残差,然后加回到变量中。

When I have this done I will repost but there may be many more questions before then. 完成此操作后,我将重新发布,但在此之前可能还会有更多问题。

Thanks again, dctb13 再次感谢,dctb13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM