2014-01-07 56 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