Excel表格中,如何做到在B列输入*,A列就自动等于对应的C列数据(如A2=C2),我不要公式,我要VBA代码

2024-12-03 20:13:06
推荐回答(4个)
回答1:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, col0%, r0&
col0 = [B1].Column
r0 = 2
For Each c In Target.Cells
If c.Column = col0 And c.Row >= r0 Then: GoTo 100
Next
Exit Sub
100:
Application.EnableEvents = False
Dim i&, col1%, col2%
col1 = [a1].Column
col2 = [c1].Column
For i = r0 To Target.Row + Target.Rows.Count - 1
If Cells(i, col0).Value = "*" Then Cells(i, col1).Value = Cells(i, col2).Value
Next
Application.EnableEvents = True
End Sub
-----------------
在sheet表名上,右键,将上面的代码复制入其中,返回sheet表,就可以实现了
注:
1、代码容错性好
2、多个单元格操作也没问题,比如,同时写入B1:B5数据
3、你没说 B列的 * 删除时的动作,需要的话,Hi我,详细解答

回答2:

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

回答3:

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

回答4:

if a2!=0
a2=c2
end if