如何使用VBA获取多行中列出的不同类型对象的STDEV值?
我正在寻找一种方法来计算相同项目代码(编号)不同时间段的总和和标准偏差。它与excel中的Subtotal函数非常相似,但是,不是将数字分组,而是创建新行并将小计插入同一列中-我想使用VBA自动执行此函数,并将小计放在相邻的列或工作表中。我已经为小计记录了一个宏,但是,我需要下一列中的stdDev,而不需要分组,也不需要破坏电子表格上的数据。我将需要其他代码的数据 如有任何建议,将不胜感激。谢谢如何使用VBA获取多行中列出的不同类型对象的STDEV值?,vba,excel,Vba,Excel,我正在寻找一种方法来计算相同项目代码(编号)不同时间段的总和和标准偏差。它与excel中的Subtotal函数非常相似,但是,不是将数字分组,而是创建新行并将小计插入同一列中-我想使用VBA自动执行此函数,并将小计放在相邻的列或工作表中。我已经为小计记录了一个宏,但是,我需要下一列中的stdDev,而不需要分组,也不需要破坏电子表格上的数据。我将需要其他代码的数据 如有任何建议,将不胜感激。谢谢 Sub stdDeviation(RN) Dim FirstOccurrence As Long
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
子标准偏差(RN)
只要
时间越长越好
作为整数的Dim i
RN2=RN
C=板材(“CONETRAN”).单元(RN2,2)
直到板材(“CONETRAN”)。单元(RN2,2)C
RN2=RN2+1
环
RN2=RN2-1
第一次出现=RN
LastOccurrence=RN2
对于i=1到最后一次出现
表格(“conedetail”).Cells(RN,16).Value=Application.WorksheetFunction.StDev(范围(“J”&FirstOccurrence&“:J”&LastOccurrence))
下一个
端接头
假设B行中有一个从
$B$1:$B44931开始的实心数据块,并且假设B列中的所有值都已排序,则可以使用以下方法获得所需的结果:
查找物料代码(编号)第一次出现的行号
查找项目代码(编号)最后一次出现的行号
在使用前两点指定的范围内执行StDev操作,并写入您喜欢的任何单元格
Cells(1, 17).Value = Application.WorksheetFunction.StDev(Range("J" & FirstOccurrence & ":J" & LastOccurrence))
您可能需要将这一切嵌套在一个循环中,这样您就可以继续在最后计算的StDev下面写入,并循环使用所有项目代码
为了让您的生活更轻松,我建议添加一个新列,复制列B中的所有值。选择所有新复制的单元格,单击“数据”选项卡-->删除重复项。现在在For循环中使用此范围的单元格来运行搜索
更新:
好的,所以你的代码是一个很好的尝试,但是有一些地方是错误的。主要有两件事:
您需要创建一个处理第一个数据点的条件处理程序(If语句
)。因为您是从“B1”开始的,所以第一次出现的FirstOccurrence
必须是B2-1=B1
您需要创建一个条件处理程序(If语句
),用于处理只有一个数据点的实例(即,当起点和终点都引用同一行时)
请尝试以下代码:
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
如果有10000个不同的项目编号,我将如何应用此方法?我需要在同一数据块中使用多个编号,而不是搜索特定编号的第一个和最后一个匹配项。这意味着,[编号]不能是特定的,可以在每次运行代码检索此数据时更改。换句话说“如果[编号]列(B)中的数据相同,则计算(J)列中的STDEV。请参阅我的答案的最后一段。您需要创建一个包含所有不同项目编号的新列,对其进行排序,然后删除重复项。现在使用该数据集来遍历每个唯一的项目编号。而不是硬编码“47-901-049W2”您可以分配单元格值,并在整个列中循环。例如:.Find(What:=Cells(x,y).value)“谢谢您的建议。我已经修改了代码(见上文)以链接到我的其他代码。但是,我现在遇到一个运行时错误1004”“无法获取工作表类的StDev属性。”“。你知道我为什么会收到这个错误吗?你能发布你的代码片段吗?哪一行给了你这个错误?忽略不计。只要我拼写正确,代码就可以正常工作。我在事件中漏掉了一个r。否则,你就帮了大忙。”。
Cells(1, 17).Value = Application.WorksheetFunction.StDev(Range("J" & FirstOccurrence & ":J" & LastOccurrence))
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