如果那么基于值的连续单元格

时间:2016-09-23 18:41:17

标签: excel vba cells

我有一排具有不同值的单元格。我期待找到两个指定值之间的三个连续单元格并使用和If Then来设置条件。

有更好的方法吗?

set Bottom = 275
set Top = 1600

 For i = 4 To LastRow
        Set r1 = Cells(5, i)
        Set r2 = r1.Offset(0, 1)
        Set r3 = r1.Offset(0, 2)
        If r1.Value < Top And r2.Value < Top and r3.Value < Top Then
        'Do something
        Elseif r1.Value > Bottom And r1.Value < Top And r2.Value > Bottom And r2.Value < Top And r3.Value > Bottom and r3.Value < Top Then 
        'Do something
        Else
        'Do something
        End If
Next i

2 个答案:

答案 0 :(得分:0)

这是一个非常小的代码段,所以要弄清楚你的代码到底在做什么有点困难。您几乎肯定会想要使用循环来遍历单元格:

How to loop through a list of data on a worksheet by using macros in Excel

我会将计算数据有效性的逻辑移到function,以便它类似于:

Function InValidRange(Value as integer)

  Dim Top As integer
  Dim Bottom as integer
  Top= 1600
  Bottom = 275
  If Value < Top and Value > Bottom Then
    InValidRange = true
  Else
    InValidRange = false
  End If
End Function

If InValidRange(r1.Value) And InValidRange(r3.Value) And InValidRange(r3.Value) then
'Do something
End If

答案 1 :(得分:0)

您可能想尝试调整此代码(已评论):

create sequence MYSEQ_sequence
start with 100
increment by 5;
相关问题