重命名宏

时间:2014-03-04 18:41:31

标签: excel vba excel-vba

我在一张纸上使用了两次相同的宏。除非我重命名,否则表格不会让我同时使用。每当我尝试它就会停止运作。

下面你会找到我想要使用的两个程序。谁能告诉我如何重命名第一个以便继续工作?

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'when entering data in a cell in Col A
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 1 Then
    n = Target.Row
    If Me.Range("A" & n).Value <> "" Then
        Me.Range("B" & n).Value = Format(Now, "hh:mm:ss AM/PM")
    End If
End If
enditall:
Application.EnableEvents = True
End Sub


Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'when entering data in a cell in Col C
On Error GoTo enditall
Application.EnableEvents = False
If Target.Cells.Column = 3 Then
    n = Target.Row
    If Me.Range("C" & n).Value <> "" Then
        Me.Range("D" & n).Value = Format(Now, "hh:mm:ss AM/PM")
    End If
End If
enditall:
Application.EnableEvents = True 
End Sub

1 个答案:

答案 0 :(得分:0)

无法重命名Worksheet_Change。而是将所有代码放在一个。

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

On Error GoTo enditall
Application.EnableEvents = False

'when entering data in a cell in Col A
If Target.Cells.Column = 1 Then
    n = Target.Row
    If Me.Range("A" & n).Value <> "" Then
        Me.Range("B" & n).Value = Format(Now, "hh:mm:ss AM/PM")
    End If
End If

'when entering data in a cell in Col C
If Target.Cells.Column = 3 Then
    n = Target.Row
    If Me.Range("C" & n).Value <> "" Then
        Me.Range("D" & n).Value = Format(Now, "hh:mm:ss AM/PM")
    End If
End If

enditall:
Application.EnableEvents = True
End Sub