使用VBA获取阈值

我有一列A和一列B.在列A中,我有(从A2开始)值从1-150(所以结束在A151)

在B栏中,我有价值观。 我想要得到单元格Z2中的值以上的第一个值,并在列B的单元格B153的A列中写出相应的值。最后一部分是棘手的。 如果以下4个值也高于Z2中的值,我只想写这个值。 这可能吗?

如果是这样的话,我在CY栏也有同样的东西 在这里输入图像描述

更好的解释:

我想从单元格2-151循环通过列内循环。

如果B2> Z2,但是接下来的4个连续单元格(B3-B6)> Z2,则复制A2到B153并移到下一列。 如果B2> Z2但是接下来的4不是全部> Z2,则用B3重复处理。 如果B2 <Z2,则移至B3。

如果没有,则将N / A复制到B153

可以这样做吗?

我第一次尝试:

=INDEX($A$2:$A$151,SUMPRODUCT(MATCH(1,--(B$2:B$151>$Z$2),0)),1) 

尽pipe这取得了第一个值。 我试图想到一个聪明的方法,只有第二个值也符合条件才能取第一个值。 从那里我肯定我可以扩大到第三,第四,第五等。

像这样的东西:

 Sub OutputEnergy() 'y = Columns to check: 2-25 'x = Rows to check: 2-152 'z = check the next 4 cells Dim x, y, z, check 'Clear the range where we store the #N/A or Energy Outputs Range("B153:Y153") = vbNullString For y = 2 To 25 For x = 2 To 152 If Cells(x, y) > Range("Z2") Then 'If value is greater than Z2 check = True 'Let's check the next 4 For z = 1 To 4 'If any of them fail If Cells(x + z, y) < Range("Z2") Then check = False 'The check fails Exit For End If Next z If check = True Then 'If the check doesn't fail Cells(153, y) = Cells(x, 1) 'Set cell 153 to the energy level Exit For End If End If Next x 'If no energy level was set - #N/A If Cells(153, y) = vbNullString Then Cells(153, y) = "#N/A" Next y End Sub 

编辑:作为一个function:

function用法:

=OutputEnergy(Range, Threshold, [Number of cells to check], [Using Headers?])

基本上,给它的范围来检查,给它一个门槛。

之后检查的单元格数量默认为4。

要获得“能量”,得到行号(如果使用标题,它减去1)

 Function OutputEnergy(TheRange As Range, Threshold As Variant, Optional NextCells As Integer = 4, Optional OffsetForHeader As Boolean = True) As Variant Dim c, x, check For Each c In TheRange If c.Value > Threshold Then check = True For x = 1 To NextCells If c.Offset(x, 0) < Threshold Then check = False Exit For End If Next x If check = True Then OutputEnergy = IIf(OffsetForHeader, c.Row - 1, c.Row) Exit Function End If End If Next c OutputEnergy = CVErr(xlErrNA) End Function 

再次编辑 – 输出到所有工作表:

OutputEnergyToSheet接受一个表作为参数:

 Sub OutputEnergyToSheet(TheSheet As String) 'y = Columns to check: 2-25 'x = Rows to check: 2-152 'z = check the next 4 cells Dim x, y, z, check 'Clear the range where we store the #N/A or Energy Outputs With Sheets(TheSheet) .Range("B153:Y153") = vbNullString For y = 2 To 25 For x = 2 To 152 If .Cells(x, y) > .Range("Z2") Then 'If value is greater than Z2 check = True 'Let's check the next 4 For z = 1 To 5 'If any of them fail If .Cells(x + z, y) < .Range("Z2") Then check = False 'The check fails Exit For End If Next z If check = True Then 'If the check doesn't fail .Cells(153, y) = Int(.Cells(x, 1)) 'Set cell 153 to the energy level Exit For End If End If Next x 'If no energy level was set - #N/A If .Cells(153, y) = vbNullString Then .Cells(153, y) = "#N/A" Next y End With End Sub 

OutputEnergyToAllSheets循环遍历每个工作表并调用新的子工具:

 Sub OutputEnergyToAllSheets() Dim w For Each w In ThisWorkbook.Worksheets If Not InStr(w.Name, "Total") > 0 And Not InStr(w.Name, "eV") > 0 Then OutputEnergyToSheet w.Name End If Next w End Sub 

有很多方法可以解决这个问题,但在我看来,你需要一个用户自定义函数的嵌套循环。

我们可以…

 function get_Energy_Row(cellSearch as Range, staticValue as Single) Dim cell1 as Single get_Energy_Row = "N/A" j = 1 col = cellSearch.Columns.Count Do cell1 = cellSearch(j, col) If cell1 <= staticValue Then 'do nothing, function already set to "N/A" Else For i = 1 to 4 If cellSearch(i + j, col) > staticValue Then get_Energy_Row = cellSearch(j,1) Else 'do nothing, function already set to "N/A" End If Next i End If j = j + 1 Loop Until j >= cellSearch.Rows.Count - 3 Or get_Energy_Row <> "N/A" End Function 

然后像这样在C153单元中调用你的UDF:

=get_Energy_Row($A2:B151,$Z$2) ,包含第一列。

注意美元符号,这将确保您的静态检查总是Z2

逻辑是我默认单元格为“N / A”,直到find一个覆盖“N / A”的标准,在这种情况下,循环被打破。

你现在的公式可能适用于单个值的情况,但我认为试图扩大规模将是一个笨拙的笨拙。 几个快速的方法来完成这个通过公式是:

 =MIN(IF(COUNTIF(INDIRECT("B"&ROW(2:147)&":B"&ROW(6:151)),">"&Z2)=5,$A2:$A147,1E9)) 

和:

 =MIN(IF((B2:B147>Z2)*(B3:B148>Z2)*(B4:B149>Z2)*(B5:B150>Z2)*(B6:B151>Z2),$A2:$A147,1E9)) 

就我个人而言,我认为后者更容易阅读和拖动电子表格(虽然前者可以修改为拖动一样简单)。 后者也避免了易失性的INDIRECTfunction。 第一个函数一次取5个单元格范围,并计算符合条件的单元格的数量。 如果我们的计数是5,我们有一场比赛。 如果您正在查找较大的匹配项,则此方法是首选。 第二个公式只是通过范围检查从r到r + 4,其中r是当前行。 这两个数组公式都应该用CTRL + SHIFT + ENTER而不是ENTER来input。