您需要的是工作表更改,而不是工作表选择更改。
还有一些逻辑冲突。如果列A包含
二者都
一个X和一个Y?此例程对A列中的最后一个值进行“隐藏列”决策。
Private Sub Worksheet_Change(ByVal Target As Range)
if not intersect(target, range("a:a")) is nothing then
on error goto safe_exit
application.enableevents = false
dim t as range
for each t in intersect(target, range("a:a"))
select case ucase(t.value)
case "X"
Columns("B:C").EntireColumn.Hidden = True
Columns("D:E").EntireColumn.Hidden = false
case "Y"
Columns("B:C").EntireColumn.Hidden = false
Columns("D:E").EntireColumn.Hidden = true
case else
'do nothing
end select
next t
End If
safe_exit:
application.enableevents = true
End Sub