Excel VBA-Worksheet_Calculate()-在源工作表中手动更新数据时出错

时间:2018-06-23 12:13:18

标签: excel-vba vba excel

在Excel VBA中,编写worksheet_calculate过程是根据同一工作表中从另一个工作表中查找的同一工作表中各个单元格中的值来更改形状的颜色。

列表框(窗体控件)用于多列数据。

该代码可用于现有数据。

每当手动键入源值时,VBA都会引发错误

screenshot

代码

Option Explicit

Private Sub Worksheet_Calculate()
    Dim Target As Range
    Set Target = Range("T6:T35")
    Dim v1 As Integer
    Dim v2 As Integer
    Dim s As Integer
    Dim t As Integer
    Dim i As Integer
    Dim Shapename As String
    If Intersect(Target, Range("T6:T35")) Is Nothing Then Exit Sub
        v1 = Left(Range("N6"), 2)
        v2 = Right(Range("N6"), 2)
        s = 19
        t = 20
        For i = 6 To 35
            Shapename = Cells(i, s)
            If Cells(i, t) < Range("N5") Then
                ActiveSheet.Shapes(Shapename).Fill.ForeColor.RGB = vbRed
            ElseIf Cells(i, t) >= v1 And Cells(i, t) < v2 Then
                ActiveSheet.Shapes(Shapename).Fill.ForeColor.RGB = vbYellow
            ElseIf Cells(i, t) >= Range("N7") Then
                ActiveSheet.Shapes(Shapename).Fill.ForeColor.RGB = vbGreen
            End If
        Next i
End Sub

0 个答案:

没有答案