我怎样才能使用VBA得到不同types的对象在多行中列出的值的STDEV?

我正在寻找一种方法来计算相同项目代码(数字)的不同时间段的总和和标准差。 这与Excel中的Subtotal函数非常相似,但是,不是将数字分组,而是创build一个新行,并将小计插入到同一列中 – 我想使用VBA自动执行此function,并将小计放在相邻的列或表中。 我已经logging了一个小计的macros,但是,我需要stdDev在下一列没有分组,或打破电子表格上的数据。 我将需要这些数据的其他代码。

任何build议将不胜感激。 谢谢

Sub stdDeviation(RN) Dim FirstOccurrence As Long Dim LastOccurrence As Long Dim i As Integer RN2 = RN C = Sheets("CONETRAN").Cells(RN2, 2) Do Until Sheets("CONETRAN").Cells(RN2, 2) <> C RN2 = RN2 + 1 Loop RN2 = RN2 - 1 FirstOccurrence = RN LastOccurrence = RN2 For i = 1 To LastOccurrence Sheets("conedetail").Cells(RN, 16).Value = Application.WorksheetFunction.StDev(Range("J" & FirstOccurrence & ":J" & LastOccurrence)) Next End Sub 

在这里输入图像说明

在这里输入图像说明

假设行B中有一个固定的数据块,从$B$1:$B44931 ,并且假设列B中的所有值都已经sorting,则可以使用以下方法获得所需的值:

  1. 查找项目代码第一次出现的行号(Number)

      Dim FirstOccurrence As Long FirstOccurrence = Range("B:B").Find(What:="47-901-049W2", After:=[B1], SearchDirection:=xlNext).Row 
  2. 查找最后一次出现的物品代码的行号(数量)

     Dim LastOccurrence As Long LastOccurrence = Range("B:B").Find(What:="47-901-049W2", After:=[B1], SearchDirection:=xlPrevious).Row 
  3. 在使用前两个点分配的范围上执行StDev操作并写入任何您喜欢的单元格

     Cells(1, 17).Value = Application.WorksheetFunction.StDev(Range("J" & FirstOccurrence & ":J" & LastOccurrence)) 

您可能需要将其全部嵌套在一个循环中,以便在最后一次计算的StDev下方继续写入,以便在所有项目代码中循环。

为了让你的生活更轻松,我build议添加一个新的列,复制列B中的所有值。select所有新复制的单元格,单击数据选项卡 – >删除重复项。 现在在For Loop使用这个单元格范围来运行你的search。

更新:

好的,所以你的代码是一个很好的尝试,但是它有一些问题。 两个主要的事情是:

  1. 您需要创build处理第一个数据点的条件处理程序( If Statement )。 既然你是从“B1”开始,那么第一个FirstOccurrence需要是B2-1 = B1

  2. 您需要创build一个条件处理程序( If Statement )来处理只有一个数据点的实例(即,当起点和终点都引用同一行时)。

试试这个代码:

 Sub stdDeviation() Dim FirstOccurrence As Long Dim LastOccurence As Long Dim RN As Range Dim workingRange As Range Dim UniqueRange As Range Dim i As Long Set workingRange = Sheets("conedetail").Range("B1:B49999") Set UniqueRange = Sheets("conedetail").Range("G1:G5") 'Insert the location of the extra column you created with all unique item codes i = 1 'This sets up your writing position 'This loops throug all the unique item numbers and retrieves and calculates the necessary data For Each RN In UniqueRange 'Need to place a control factor in for the very first set of data If RN.Row = 1 Then FirstOccurrence = workingRange.Find(What:=RN.Text, After:=[B1], SearchDirection:=xlNext).Row - 1 Else FirstOccurrence = workingRange.Find(What:=RN.Text, After:=[B1], SearchDirection:=xlNext).Row End If LastOccurence = workingRange.Find(What:=RN.Text, After:=[B1], SearchDirection:=xlPrevious).Row 'Tests to see if only one occurrence, if no calculates stDev normally If LastOccurence - FirstOccurrence = 0 Then 'Insert whatever you want it to do here if there is only one data point Sheets("conedetail").Cells(i, 16).Value = 0 Else Sheets("conedetail").Cells(i, 16).Value = Application.WorksheetFunction.StDev(Range("J" & FirstOccurrence & ":J" & LastOccurence)) End If i = i + 1 Next RN Set RN = Nothing Set workingRange = Nothing Set UniqueRange = Nothing End Sub