2014-01-07 58 views
-2

我想要基于下面的代码在我的工作表内更改几列,但是我不断收到一个错误代码。VB-新的VB Excel

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Column = 12 Then 
     Application.EnableEvents = False 
     Cells(Target.Row, 13).Value = Date + Time 
     Application.EnableEvents = True 
    End If 
    If Target.Column = 13 Then 
     Application.EnableEvents = False 
     Cells(Target.Row, 14).Value = Date + Time 
     Application.EnableEvents = True 
    End If 
End Sub 
+3

什么是您收到的错误? –

+2

什么是日期?时间是什么? –

+0

我怀疑这实际上是一个关于Excel中VBA的问题,而不是关于VB.NET的问题。如果是这样,请修复标签。 –

回答

0

服用野生刺,但我假设你想格式化为以下

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target.Column = 12 Then 
    Application.EnableEvents = False 
    Cells(Target.Row, 13).Value = Format(DateTime.Now, "mm/dd/yyyy") 
    Application.EnableEvents = True 
    End If 
    If Target.Column = 13 Then 
    Application.EnableEvents = False 
    Cells(Target.Row, 14).Value = Format(DateTime.Now, "hh:mm:ss") 
    Application.EnableEvents = True 
    End If 
End Sub