Excel表格中如何修改VBA代码,做到在如果C列中有数据,保留不变,没有,按条件输入数据新的数据

2024-12-12 14:12:31
推荐回答(3个)
回答1:

If Target.Column = 7 And Cells(Target.Row, 1).Value <> Cells(Target.Row - 1, 1).Value And Cells(Target.Row, 3).Value = "" Then
Cells(Target.Row, 3).Value = Now()
ElseIf Target.Column = 7 And Cells(Target.Row, 1).Value = Cells(Target.Row - 1, 1).Value Then
Cells(Target.Row, 3).Value = Cells(Target.Row - 1, 3)
Cells(Target.Row, 3).NumberFormatLocal = "yyyy-m-d h:mm"
End If

回答2:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 Or Target.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
Select Case Target.Column
Case Is = 7
If Target.Value > 0 Then
With Target.Offset(0, -4)
If .Value > 0 Then
Else
If Target.Offset(0, -6) = Target.Offset(-1, -6) Then .Value = Target.Offset(-1, -4).Value
End If
If .Value > 0 Then
Else
.Value = Now
.NumberFormatLocal = "yyyy-m-d h:mm;@"
End If
End With
End If
End Select
Application.EnableEvents = True
End Sub

回答3:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Row > 1 And Target = "*" Then
Cells(Target.Row, 1) = Cells(Target.Row, 3)
End If
End Sub