我创建了一个表单,让用户可以选择7种不同的选项,这些选项都是默认的空白。当他们点击选项旁边的单元格时,它将从空白变为"是"再次点击它将删除文本,依此类推。单元格R33和S33合并,代码在那里工作正常,但我需要代码运行多个单元格,这些单元格也合并,如(R35-S35,R37-S37,R39-S39和R41-S41。
你可以帮我解决这个问题吗?
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("R33").MergeArea) Is Nothing Then
Select Case True
Case Target.Cells(1) = "yes"
Target.Cells(1) = ""
Case Target.Cells(1) = ""
Target.Cells(1) = "yes"
End Select
Range("A1").Select
End If
Application.EnableEvents = True
End Sub
答案 0 :(得分:2)
您可以选择多个单元格,并且应该考虑这些单元格。合并区域的静态联合不必在每个选择上重新定义。
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
static mrng as range
if mrng is nothing then
set mrng = union(cells(33, "R").mergearea, cells(35, "R").mergearea, _
cells(37, "R").mergearea, cells(39, "R").mergearea, _
cells(41, "R").mergearea)
end if
If Not Intersect(Target, mrng) Is Nothing Then
Application.EnableEvents = False
dim t as range
for each t in Intersect(Target, mrng)
select case lcase(t.value2)
case "yes"
t = vbnullstring
case else
t = "Yes"
end select
next t
Range("A1").Select
End If
Application.EnableEvents = True
End Sub