如何从另一个数组excel vba的计算中创build一个数组

我是新来的编码,并试图通过VBA学习。 我正在试图做的是按照程序计算数据集中的exception值。 我的麻烦是试图确定数据集中离平均值(离群值)最远且循环了k次的元素。 大部分的代码是非常混乱的,因为我一直在试图找出什么是错误的,所以忽略MsgBox和丑陋的格式。 在我的代码的最后一部分,我试着从DataSet中取出这些元素,然后从这些元素中减去它们,并将这些值存储在一个新的数组中。 之后,我会采取差异数组中的元素的绝对值,并将它们存储在一个新的数组(Diff2)中。 我知道我可以绕过Diff2只需要计算Diff的绝对值。 当我运行代码时,我得到了types不匹配的错误,经过一番调查,我意识到Diff(和Diff2)不是数组。 如果有人知道我可以怎么做一个数组或一个更好的解决方法,这将是非常感谢!

Sub CalculateOutliers() Dim n As Integer Dim mean As Double Dim SD As Double Dim X As Integer Dim k As Integer Dim DataSet As Variant Dim ESDPrin As Double DataSet = Selection.Value 'Copies highlighted data into DataSet variable 'Cell A1 is (1,1) Because it starts at 0 which is out of range n = Selection.CountLarge 'Counts number of entries 'If n < 20 Then 'MsgBox "Data set too small" 'Exit Sub 'End If 'Ends Subroutine if data set is too small for this analysis If n < 50 Then k = Int(n / 10) Else k = 5 End If 'determines k = number of possible outliers mean = Application.WorksheetFunction.Average(DataSet) 'Calculates mean of Data Set MsgBox mean & "Average" SD = Application.WorksheetFunction.StDev(DataSet) 'Calculates Standard Deviation of Data Set Dim element As Variant Dim Diff As Variant For Each element In DataSet Diff = element - mean MsgBox Diff & " Difference" Next element Dim P As Integer Dim Outlier As Integer Dim Diff2 As Variant Diff2 = Abs(Diff) For P = 1 To k Outlier = UBound(Diff, 1) MsgBox Outlier Next P End Sub 

这里如何创build大小为n的差分数组

 ReDim Diff(1 To n) As Double Dim i As Long For Each element In DataSet i = i + 1 Diff(i) = element - mean Next element 

不过,我不认为这是正确的做法。 不需要Diff数组。 你应该做的是,一旦你计算了meanSD偏差,在DataSet数组本身上迭代,检查每个元素与平均值的绝对差值,除以stdev,并将这个比率与某个阈值(比如2或3)进行比较决定这个元素是否是一个exception值,在这种情况下,你将其作为exception值打印出来。 像这样的东西:

 For Each element In DataSet If abs(element - mean) / SD > 3 Then Debug.Print "outlier: " & element Next element 

我认为代码会是这样的

 Sub CalculateOutliers() Dim n As Integer Dim mean As Double Dim SD As Double Dim X As Integer Dim k As Integer Dim DataSet As Variant Dim ESDPrin As Double DataSet = Selection.Value 'Copies highlighted data into DataSet variable 'Cell A1 is (1,1) Because it starts at 0 which is out of range n = Selection.CountLarge 'Counts number of entries 'If n < 20 Then 'MsgBox "Data set too small" 'Exit Sub 'End If 'Ends Subroutine if data set is too small for this analysis If n < 50 Then k = Int(n / 10) Else k = 5 End If 'determines k = number of possible outliers mean = Application.WorksheetFunction.Average(DataSet) 'Calculates mean of Data Set MsgBox mean & "Average" SD = Application.WorksheetFunction.StDev(DataSet) 'Calculates Standard Deviation of Data Set Dim element As Variant Dim Diff() As Variant, Diff2() As Variant, j As Integer For Each element In DataSet j = j + 1 ReDim Preserve Diff(1 To j): ReDim Preserve Diff2(1 To j) Diff(j) = element - mean Diff2(j) = Abs(Diff(j)) MsgBox Diff(j) & " Difference" MsgBox Diff2(j) & " Difference abs " Next element MsgBox UBound(Diff) 'Dim P As Integer 'Dim Outlier As Integer 'Dim Diff2 As Variant End Sub