VBA 单元格中的默认值基于另一个单元格

VBA Default value in cell based on another cell

我是 VBA 的新手,但发现这段代码插入了默认值。问题是我需要在基于另一列的列中插入一个默认值。

假设在 "Column A" 中,如果该行处于活动状态,则取值 1,如果该行不活动,则取值 0。如果列 "A1" = 1 它应该在列 "C1" 中插入 9999 如果是 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

再次感谢

查看这是否是您要找的东西:

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

如果您还需要其他内容,请进一步说明。

尝试以下操作。它基于检测 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