Option Explicit
Private newVal
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.cells.count > 1 Then Exit Sub
If Target.column = 1 Then
If Target.Offset(, 1).Value <> 0 Then
Application.EnableEvents = False 'to avoind triggering again the Change event
newVal = Target.Value
Application.Undo
Application.EnableEvents = True
End If
End If
End Sub
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const TGT_FIRST_CELL As String = "A2"
Const SRC_COLUMN As String = "B"
Dim trg As Range
With Me.Range(TGT_FIRST_CELL)
Set trg = .Resize(Me.Rows.Count - .Row + 1)
End With
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
Dim ColumnOffset As Long
ColumnOffset = Me.Columns(SRC_COLUMN).Column - irg.Column
Dim srg As Range: Set srg = irg.Offset(, ColumnOffset)
If srg.Cells.Count = 1 Then
Dim sValue As Variant: sValue = srg.Value
If VarType(sValue) = vbDouble Then ' write just if 0
' or:
'If IsNumeric(sValue) Then ' write if 0 or empty
If sValue = 0 Then Exit Sub
End If
End If
srg.Cells(1).Select
End Sub
3条答案
按热度按时间nnsrf1az1#
请在相应的工作表代码模块中复制下一个事件代码,并尝试更改:
z31licg02#
A工作表选择变更:限制列访问
A2
,B
),仅当要选择单个单元并且列B
的同一行中的单元等于0
时,以下将允许访问列A
(更准确地说,访问A2:A1048576
)。B
中的单元格为空,它也将允许访问。0
,但不是 Double 类型,而是 Empty 类型。板材模块,例如
Sheet1
(不进入ThisWorkbook
或标准模块,例如Module1
)ffx8fchx3#
您可以为此使用数据验证:
选择范围(例如:x1月1x)
转到数据-〉数据工具-〉数据验证
选择以下选项:
允许"自定义"
分子式:
=B1:B10=0