|
楼主 |
发表于 2024-10-22 20:59
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Dim colorCycle As Variant
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
' 检查双击的单元格是否在A1:E1区域内
If Not Intersect(Target, Me.Range("A1:E1")) Is Nothing Then
' 初始化颜色循环(如果尚未初始化)
If IsEmpty(colorCycle) Then
colorCycle = Array(RGB(255, 0, 0), RGB(0, 255, 0), RGB(0, 0, 255))
End If
' 找到当前颜色的索引并更新为下一个颜色
Dim currentIndex As Integer
For currentIndex = LBound(colorCycle) To UBound(colorCycle)
If Target.Interior.Color = colorCycle(currentIndex) Then
Exit For
End If
Next currentIndex
' 如果当前索引超出了颜色数组的范围(理论上不应该发生,除非单元格被手动设置了其他颜色)
' 则重置为第一个颜色
If currentIndex > UBound(colorCycle) Then
currentIndex = LBound(colorCycle)
End If
' 应用下一个颜色
Target.Interior.Color = colorCycle((currentIndex + 1) Mod (UBound(colorCycle) + 1))
' 取消双击后的默认编辑行为(可选)
Cancel = True
End If
End Sub
Private Sub Workbook_Open()
' 清除任何可能残留的颜色循环变量(虽然在这个特定例子中可能不是必需的,因为每次都会重新初始化)
colorCycle = Empty
End Sub
|
|