如何调整此代码,以便它适用于整个列而不仅适用于一个单元格?
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.EnableEvents = False
With Target
If .Address = Range("A11:A1").Address Then
Select Case .Value
Case "Excel"
.Value = "Word"
Case "Word"
.Value = "Outlook"
Case "Outlook"
.Value = "Excel"
Case Else
.Value = "Word"
End Select
End If
End With
Range("A2").Select
Application.EnableEvents = True
End Sub
非常感谢! Jeame
答案 0 :(得分:0)
如下所示,如果您在结尾选择A2并且目标是A列,那么您正在设置重复链。我已删除。
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Application.EnableEvents = False
If Target.Column = 1 Then 'example column A
With Target
Select Case .Value
Case "Excel"
.Value = "Word"
Case "Word"
.Value = "Outlook"
Case "Outlook"
.Value = "Excel"
Case Else
.Value = "Word"
End Select
End With
End If
Application.EnableEvents = True
End Sub
编辑:
在更改要求之后,请查看您的代码的重写。 Test子项仅用于测试事件。
Option Explicit
Private Sub Test()
Call Worksheet_BeforeDoubleClick(Selection, True)
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 2 Then
Dim var As Long
Dim targetRange As Range
var = Target.Row
Set targetRange = ActiveSheet.Range("A" & var & ":B" & var) '2 columns of interest for row that was triggered
With targetRange.Columns(2)
Select Case LCase(.Value) 'i.e. column B's value. Add add change to lowercase so test always matches
Case "active"
targetRange.Interior.ColorIndex = 16
.Value = "Finished"
Case "wip"
targetRange.Interior.ColorIndex = 2
.Value = "Done"
End Select
End With
End If
End Sub
答案 1 :(得分:0)
对我来说太复杂的解决方案。下面是代码,它应该类似,但双击。之前我只做了数据验证 - 列表框有两个选项。但不知怎的,它对我不起作用。
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Var = Target.Row
var2 = "b" + Var
If (Target.Column = 2) Then
If (Range(var2).Value = "Active") Then
var2 = "a" + Var
Range(var2).Interior.ColorIndex = 16
var2 = "b" + Var
Range(var2).Interior.ColorIndex = 16
Range(var2).Value = "Finished"
Else
Var = "b" + Var
If (Range(var2).Value = "WIP") Then
var2 = "a" + Var
Range(var2).Interior.ColorIndex = 2
var2 = "b" + Var
Range(var2).Interior.ColorIndex = 2
Range(var2).Value = "DONE"
End If
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub