我正在尝试制作一个“旋转按钮”,该按钮将编辑活动单元格,但仅当它在特定单元格范围内时才编辑。如果活动单元格在工作表1单元格J63:J97的范围内,我希望代码启动,如果不在该范围内,则代码不运行。
这是我到目前为止的代码。它将根据需要编辑活动单元格。但是,它不限于我需要的范围。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
SpinButton1.Value = Selection.Value
End Sub
Private Sub SpinButton1_Change()
Selection.Value = SpinButton1.Value
End Sub
答案 0 :(得分:1)
尝试使用Application.Intersect
。
我已经定义了一个单独的Function
来完成这项工作。
此代码已经过测试,并且有效:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If checkIntersection(Target, Range("J63:J97")) Then
SpinButton1.Value = Selection.Value
End If
End Sub
Private Sub SpinButton1_Change()
If checkIntersection(Selection, Range("J63:J97")) Then
Selection.Value = SpinButton1.Value
End If
End Sub
'Check if Range1 and Range2 are intersecting
Function checkIntersection(range1 As Range, range2 As Range) As Boolean
checkIntersection = Not Application.Intersect(range1, range2) Is Nothing
End Function
答案 1 :(得分:0)
可以使用Intersect
Private Sub SpinButton1_Change()
If Not Intersect(Selection, Range("J63:J97")) Is Nothing Then
Selection.Value = SpinButton1.Value
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("J63:J97")) Is Nothing Then
SpinButton1.Value = Selection.Value
End If
End Sub