当行数改变时使用worksheet_calculate

时间:2018-06-14 05:39:53

标签: excel vba excel-vba

我在x和y列中有一系列数据:

image of data in column x and y

我想将列x与y进行比较,并在x>时发出通知2Y。这是代码:( P.S.此代码运行完美)。

Private Sub Worksheet_Calculate()

If Range("A2").Value <> PrevValb Then
  PrevValb = Range("A2").Value
  If Range("A2").Value > 2 * Range("B2").Value Then
    Dim result1 As VbMsgBoxResult
    result1 = MsgBox("A2 has exceeded range", vbOKCancel, "Range Exceeded")
    If result1 = vbCancel Then
      Stop
  End If
  End If
End If

If Range("A3").Value <> PrevValc Then
  PrevValc = Range("A3").Value
  If Range("A3").Value > 2 * Range("B3").Value Then
    Dim result2 As VbMsgBoxResult
    result2 = MsgBox("A3 has exceeded range", vbOKCancel, "Range Exceeded")
    If result2 = vbCancel Then
      Stop
    End If
  End If
End If

If Range("A4").Value <> PrevVald Then
  PrevVald = Range("A4").Value
  If Range("A4").Value > 2 * Range("B4").Value Then
    Dim result3 As VbMsgBoxResult
    result3 = MsgBox("A4 has exceeded range", vbOKCancel, "Range Exceeded")
    If result3 = vbCancel Then
      Stop
    End If
  End If
End If

If Range("A5").Value <> PrevVale Then
  PrevVale = Range("A5").Value
  If Range("A5").Value > 2 * Range("B5").Value Then
    Dim result4 As VbMsgBoxResult
    result4 = MsgBox("A5 has exceeded range", vbOKCancel, "Range Exceeded")
    If result4 = vbCancel Then
      Stop
    End If
  End If
End If

End Sub

但每天的行数会发生变化,单元格值也会发生变化(A2,A3,A4,A5)。我为列x和y中的所有值创建了一个NamedRange。有没有办法让细胞价值变得动态?我想使用一个整数循环,其中每个整数引用行号,循环继续,直到它到达最后一行。但我不确定如何将其与现有代码结合起来。

如果您需要任何澄清,请与我们联系。

2 个答案:

答案 0 :(得分:1)

你需要循环你的代码,因为最后一行是这样的:

Private Sub Worksheet_Calculate()

Dim rangeName As String
rangeName = "NamedRange"

firstRowNamedRange = Range(rangeName).Row
lastRowNamedRange = Range(rangeName).Row + Range(rangeName).Rows.count


    For i = firstRowNamedRange To lastRowNamedRange Step 1

        If Range("A" & i).Value <> PrevValb Then
            PrevValb = Range("A" & i).Value
                If Range("A" & i).Value > 2 * Range("B2" & i).Value Then
                    Dim result1 As VbMsgBoxResult
                    result1 = MsgBox("A" & i & " has exceeded range", vbOKCancel, "Range Exceeded")
                    If result1 = vbCancel Then
                    Stop
                     End If
                End If
        End If


    Next i

End Sub

答案 1 :(得分:1)

感谢@JC Guidicelli,这是完美的最终代码!

Sheet 1中:

Private Sub Worksheet_Calculate()

Dim rangeName As String
rangeName = "RangeOfValues"

firstRowNamedRange = Range(rangeName).Row
lastRowNamedRange = Range(rangeName).Row + Range(rangeName).Rows.Count - 1

For i = firstRowNamedRange To lastRowNamedRange Step 1

    If Range("A" & i).Value <> PrevValb Then
        PrevValb = Range("A" & i).Value
            If Range("A" & i).Value > 2 * Range("B" & i).Value Then
                Dim result As VbMsgBoxResult
                result = MsgBox("A" & i & " has exceeded range", vbOKCancel, "Range Exceeded")
                If result = vbCancel Then
                Stop
                End If
            End If
    End If

Next i

End Sub

模块:

Public PrevValb As Variant