我对VBA很新,但发现此代码插入了默认值。问题是我需要根据另一列插入一个默认值。
在" A列和#34;中说如果行处于活动状态,则取值1;如果行处于非活动状态,则取值为0。如果列" A1" = 1它应该在列中插入9999" C1"如果为0则不应该做任何事情。任何人都可以帮我修改代码吗?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range, inter As Range, r As Range
Set C = Range("C9:C21")
Set inter = Intersect(C, Target)
If inter Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In inter
If r.Value = "" Then r.Value = 9999
Next r
Application.EnableEvents = True
End Sub
再次感谢你
答案 0 :(得分:1)
尝试以下方法。它基于检测B列的变化并根据A设置C.我已从单元格B1设置但您可以根据需要进行调整。
并非它非常相关,但我检查了与this
相交的语法Private Sub Worksheet_Change(ByVal Target As Range)
Dim aRange As Range
Set aRange = ActiveSheet.Range("B1:B21") 'Changed to B1 start
If Not Application.Intersect(Target, aRange) Is Nothing Then
If Target.Offset(, -1) = 1 Then
Target.Offset(, 1) = 9999
ElseIf Target.Offset(, -1) = 0 Then
Target.Offset(, 1) = vbNullString
End If
End If
End Sub
答案 1 :(得分:0)
检查这是否是您正在寻找的东西:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range, inter As Range, r As Range
Set C = Range("C9:C21")
Set inter = Intersect(C, Target)
If inter Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In inter
'this checks cell on the same row as r in column A
If cells(r.row,1) = "Whatever values you need here" Then r.Value = 9999
end if
Next r
Application.EnableEvents = True
End Sub
如果您还想要其他内容,请进一步说明。