2014-05-16 110 views
-1

需要這個宏幫助:創建時間戳VBA

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

Sub DeleteCells() 
For Each Cell In Range("B3:B25") 
    If IsBlank(Cell.Value) Then 
     Cell.Offset(0, 1).Clear 
    End If 
Next 
End Sub 

這個宏的目的是爲了創造一個時間戳。第一個宏工作正常。如果填入行B中的任何內容,則會在行C中創建時間戳。但是,刪除單元格功能不起作用。我想要它,以便如果有人刪除B行中的單元格,時間戳也將被刪除。

回答

1

試試這個:

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim rng As Range, c As Range 

    'anything in ColB? 
    Set rng = Application.Intersect(Me.Columns(2), Target) 
    If rng Is Nothing Then Exit Sub 'nothing to process... 

    Application.EnableEvents = False 
    'could be >1 cell, so loop over them... 
    For Each c In rng.Cells 
     'skip any cells with errors 
     If c.Row>=3 And Not IsError(c.Value) Then '<<edit 
      c.EntireRow.Cells(3).Value = _ 
       IIf(Len(c.Value) > 0, Now, "") 
     End If 
    Next c 
    Application.EnableEvents = True 

End Sub 
+0

這是太棒了,謝謝!不過,我想在B3開始檢查第2欄。我怎樣才能解決這個問題? – gatesofnm

+1

看我的編輯檢查行。 –