进行更改时更新文本框值

Update Textbox value when changes are made

我使用下面的代码来计算空白且具有特定颜色的单元格。 然后我将计数存储在文本框中。该代码工作正常,但我必须 运行 更新文本框值的代码。我需要在进行更改时更新文本框值。

提前Tx!

Sub CountBlankColors4()
    Dim c As Range
    Dim J As Integer
    Dim ColorCount(100) As Long

    ActiveSheet.Range("A1:K30").SpecialCells(xlCellTypeBlanks).Select

    For Each c In Selection
        With c.Interior
            If .Pattern = xlSolid Then
                If .TintAndShade = -0.249977111117893 Then
                    If IsEmpty(c) Then
                        ColorCount(.ColorIndex) = _
                          ColorCount(.ColorIndex) + 1
                    End If
                End If
            End If
        End With
    Next c

    For J = 0 To 100
        If ColorCount(J) > 0 Then
            sTemp = ColorCount(J)
        End If
    Next J

    TextBox1.Value = sTemp
End Sub

虽然您的第一步可能是调查 Worksheet_Change() 事件,但可能还有另一种方法可以处理此问题。您可以创建一个可以像工作簿中的普通函数一样调用的 UDF(用户定义函数)。例如,下面将对一个范围内的单元格进行计数,并在该范围内的某些内容发生变化时进行更新。

Public Function CountCells(RNG As Range) As Double

For Each CL In RNG
    If IsEmpty(CL) = True Then
        With CL.Interior
            If .Pattern = xlSolid And .TintAndShade = -0.249977111117893 Then
                CountCells = CountCells + 1
            End If
        End With
    End If
Next CL

End Function

您可以 link 将 C1 的值添加到文本框。