使用说明:在表格中按Alt+F11,进入VBA编辑器,选择工作表,然后将下面的代码插入进去,之后保存。
代码说明:此代码将在你每次选择A列数据的时候,将选中单元格的数据自动写入B列对应行,再修改A列单元格,B列将保存之前的A单元格数据
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
hh = Target.Cells.Row
lh = Target.Cells.Column
t = Cells(hh, lh)
if lh = 1 then
Cells(hh, 2) = t
end if
Application.EnableEvents = True
End Sub
追问大神,这个代码那个位置代表A列那个代表B列,如果我想在B列和C列使用该修改哪里呢?谢谢啦!
追答Public k As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
hh = Target.Cells.Row
lh = Target.Cells.Column
If lh = 1 Then
k = Cells(hh, lh)
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
hh = Target.Cells.Row
lh = Target.Cells.Column
n = Cells(hh, lh)
If n <> k And n <> "" Then
Cells(hh, 2) = k
End If
Application.EnableEvents = True
End Sub
'VBA模仿excel修订功能