使用VBA获取阈值
我有a列和B列。在a列中,我有(从A2开始)从1到150的值(以A151结尾) 在B列中,我有值。我想得到第一个值,它高于单元格Z2中的值,并在单元格B153中的A列中为B列写出相应的值。最后一部分很棘手。我只想在以下4个值也高于Z2中的值时写入此值。这是不是可能的 如果是这样,我在C-Y列中也有同样的内容 更好的解释: 我想循环浏览B-Y列 来自单元格2-151的内环 如果B2>Z2,但接下来的4个连续单元格(B3-B6)>Z2,则将A2复制到B153并移动到下一列。 如果B2>Z2,但下一个4不是全部>Z2,则用B3重复该过程。 如果B2使用VBA获取阈值,vba,excel,Vba,Excel,我有a列和B列。在a列中,我有(从A2开始)从1到150的值(以A151结尾) 在B列中,我有值。我想得到第一个值,它高于单元格Z2中的值,并在单元格B153中的A列中为B列写出相应的值。最后一部分很棘手。我只想在以下4个值也高于Z2中的值时写入此值。这是不是可能的 如果是这样,我在C-Y列中也有同样的内容 更好的解释: 我想循环浏览B-Y列 来自单元格2-151的内环 如果B2>Z2,但接下来的4个连续单元格(B3-B6)>Z2,则将A2复制到B153并移动到下一列。 如果B2>Z2,但下一个
=INDEX($A$2:$A$151,SUMPRODUCT(MATCH(1,--(B$2:B$151>$Z$2),0)),1)
但这取第一个值。我正试图想出一个聪明的方法,只有当第二个值也符合标准时,才取第一个值。从那里我确信我可以扩展到第三、第四、第五等等。类似这样的内容:
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
再次编辑-输出到所有工作表:
OutputeneEnergyToSheet接受工作表作为参数:
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
大概是这样的:
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
再次编辑-输出到所有工作表:
OutputeneEnergyToSheet接受工作表作为参数:
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
您当前的公式可能适用于单值情况,但我认为尝试放大会有点笨拙。通过公式实现这一点的两种快速方法是:
=MIN(IF(COUNTIF(INDIRECT("B"&ROW(2:147)&":B"&ROW(6:151)),">"&Z2)=5,$A2:$A147,1E9))
以及:
就我个人而言,我认为后者更容易阅读和在电子表格中拖动(尽管前者也可以修改为拖动)。后者还避免了volatile
间接
功能。第一个函数一次获取5个单元格范围,并统计符合条件的单元格数。如果我们的计数是5,我们有一个匹配。如果要查找更大的匹配集,则首选此方法。第二个公式只是逐步通过范围检查,基本上是r到r+4,其中r是当前行。这两个都是数组公式,应该使用CTRL+SHIFT+ENTER键输入,而不是只使用ENTER键。您当前的公式可能适用于单值情况,但我认为尝试放大会有点笨拙。通过公式实现这一点的两种快速方法是:
=MIN(IF(COUNTIF(INDIRECT("B"&ROW(2:147)&":B"&ROW(6:151)),">"&Z2)=5,$A2:$A147,1E9))
以及:
就我个人而言,我认为后者更容易阅读和在电子表格中拖动(尽管前者也可以修改为拖动)。后者还避免了volatile
间接
功能。第一个函数一次获取5个单元格范围,并统计符合条件的单元格数。如果我们的计数是5,我们有一个匹配。如果要查找更大的匹配集,则首选此方法。第二个公式只是逐步通过范围检查,基本上是r到r+4,其中r是当前行。这两个都是数组公式,应该用CTRL+SHIFT+ENTER而不是只按ENTER键输入。有很多方法可以解决这个问题,但我认为用户定义函数中需要一个嵌套循环
我们可以
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
函数获取能量行(cellSearch作为范围,staticValue作为单个)
暗淡的细胞1为单细胞
获取能量行=“不适用”
j=1
col=cellSearch.Columns.Count
做
cell1=cellSearch(j,col)
如果cell1=cellSearch.Rows.Count-3或获取第行“N/A”
端函数
然后在cellC153
中调用您的自定义项,如下所示:
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
=get_Energy_Row($A2:B151,$Z$2)
,其中包含第一列
注意美元符号,这将确保您的静态支票始终为Z2
逻辑是,我将单元格默认为“N/A”,直到它找到一个覆盖“N/A”的条件,在这种情况下,循环被破坏。有很多方法可以解决这个问题,但我认为在用户定义的函数中需要一个嵌套的循环 我们可以
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
函数获取能量行(cellSearch作为范围,staticValue作为单个)
暗淡的细胞1为单细胞
获取能量行=“不适用”
j=1
col=cellSearch.Columns.Count
做
cell1=cellSearch(j,col)
如果cell1=cellSearch.Rows.Count-3或获取第行“N/A”
端函数
然后在cellC153
中调用您的自定义项,如下所示:
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
=get_Energy_Row($A2:B151,$Z$2)
,其中包含第一列
注意美元符号,这将确保您的静态支票始终为Z2
逻辑是,我将单元格默认为“N/A”,直到它找到覆盖“N/A”的条件,在这种情况下,循环被中断。请发布您的尝试solution@Barranka我发布了我尝试过的东西。工作表中另一个单元格中的简单代码使用VBA思考它的另一种方式是循环遍历每一列。从B2开始。如果B2-B6都大于Z2,则返回A2。如果没有,请转到B3并重复。一旦找到值,在B153中发布,并移动到下一列,在Y列停止。我能想到怎么做,只是在努力写任何有用的东西。@user1274820好,我发布了一个示例。突出显示的单元格用于视觉帮助。看B栏,有2个突出显示,但后面的没有突出显示?但单元格43的值与A列中的第一个值相对应,该列中的下4个值也大于Z2。这有什么意义吗?我补充了更好的解释@用户1274820有什么想法吗?请张贴您的申请表solution@Barranka我发布了我尝试过的东西。工作表中另一个单元格中的简单代码使用VBA思考它的另一种方式是循环遍历每一列。从B2开始。如果B2-B6都大于Z2,则返回A2。如果没有,请转到B3并重复。一旦找到值,在B153中发布,并移动到下一列,在Y列停止。我能想到怎么做,只是在努力写任何有用的东西。@user1274820好,我发布了一个示例。突出显示的单元格用于视觉帮助。s