举例:工作表Sheet1,指定区域为A1:N36
如何用VBA实现如下功能:
在这些指定的单元格中,点选到某个单元格一次后自动填充绿色,再次点击或者多选则清除颜色,第三次点击又可以自动填充颜色.......。请高手赐马,谢谢
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim g As Range
Set g = Intersect(Target, Range("A1:N36")) '获取交集区域g
If g Is Nothing Then Exit Sub '如果交集为空则退出sub
If g.Count > 1 Then '如果g区域包含多个单元格
g.Interior.Color = xlNone '那么设置g的背景色为无色
Else '否则,即g只有一个单元格
If g.Interior.Color = vbRed Then '如果g的背景色是红色
g.Interior.Color = xlNone '那么设置为无色
Else
g.Interior.Color = vbRed '否则设置为红色
End If
End If
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答 2022-11-25
Private Sub Worksheet_SelectionChange(ByVal Target As Range) '自动复制到 零售单
Dim tr, tc
tr = Target.Row
tc = Target.Column
If tr > 1 And tr <= 36 And tc <= 14 And Cells(tr, tc).Interior.ColorIndex = xlNone Then
Cells(tr, tc).Interior.ColorIndex = 4
Else
Cells(tr, tc).Interior.ColorIndex = xlNone
End If
End Sub
试下
Dim tr, tc
tr = Target.Row
tc = Target.Column
If tr > 1 And tr <= 36 And tc <= 14 And Cells(tr, tc).Interior.ColorIndex = xlNone Then
Cells(tr, tc).Interior.ColorIndex = 4
Else
Cells(tr, tc).Interior.ColorIndex = xlNone
End If
End Sub
试下