该复选框应将目标单元格中的数据重置为零,如果未选中,则将其返回。我想在集合中存储迭代之间的数据,但是这不起作用,所有值都为零。
有没有办法在迭代期间将这些值存储在单元格中?请帮帮我也忽略颜色,最重要的是让归零和返回单元格数据工作。
Private Sub CheckBox1_Click()
Dim ws As Worksheet
Dim cell As Range
Dim targetValue As String
Dim originalValue As Variant
Dim originalDataCollection As New Collection
targetValue = "СОК"
Set ws = ThisWorkbook.Sheets("Лист1") ' Replace with your sheet name
Set originalDataCollection = New Collection
If CheckBox1.Value = True Then
' When the checkbox is checked, change the text and background color of matching cells to white
For Each cell In ws.Range("A1:AE61")
If cell.Value = targetValue Then
originalColor = cell.Offset(0, -1).Font.Color
originalInteriorColor = cell.Offset(0, -1).Interior.Color
cell.Offset(0, -1).Font.Color = RGB(255, 255, 255) ' Change the text color to white
cell.Offset(0, -1).Interior.Color = RGB(255, 255, 255) ' Change the background color to white
cell.Offset(0, -2).Font.Color = RGB(255, 255, 255) ' Change the text color to white
cell.Offset(0, -2).Interior.Color = RGB(255, 255, 255) ' Change the background color to white
cell.Offset(0, -3).Font.Color = RGB(255, 255, 255) ' Change the text color to white
cell.Offset(0, -3).Interior.Color = RGB(255, 255, 255) ' Change the background color to white
cell.Offset(0, 0).Font.Color = RGB(255, 255, 255) ' Change the text color of the cell on the left to white
cell.Offset(0, 0).Interior.Color = RGB(255, 255, 255) ' Change the background color to white
' Store the original value of cell.Offset(0, -2)
originalValue = cell.Offset(0, -2).Value
originalDataCollection.Add originalValue
' Set the value of cell.Offset(0, -2) to zero
cell.Offset(0, -2).Value = 0
End If
Next cell
Else
' When the checkbox is unchecked, reset the text and background color to the original for matching cells
For Each cell In ws.Range("A1:AE61")
If cell.Value = targetValue Then
cell.Offset(0, -1).Font.Color = originalColor
cell.Offset(0, -1).Interior.Color = RGB(255, 0, 0) ' Reset background color to original
cell.Offset(0, -2).Font.Color = originalColor
cell.Offset(0, -2).Interior.Color = RGB(255, 0, 0) ' Reset background color to original
cell.Offset(0, -3).Font.Color = originalColor
cell.Offset(0, -3).Interior.Color = RGB(255, 0, 0) ' Reset background color to original
cell.Offset(0, 0).Font.Color = originalColor
cell.Offset(0, 0).Interior.Color = RGB(152, 212, 84)
If originalDataCollection.Count > 0 Then
cell.Offset(0, -2).Value = originalDataCollection(1)
originalDataCollection.Remove 1 ' Remove the first item from the collection
End If
End If
Next cell
End If
End Sub
1条答案
按热度按时间klr1opcd1#
Declare Sub外部的变量,以便在Sub退出时保留值。