2015-09-07 76 views
2

我試圖記錄在列A中的一個單元格中所做的多個更改。在我的示例中,我有一個用戶可以在列A5中輸入日期和時間。稍後用戶可能會更改該單元格中的值(A5)。這種變化可能發生5次。如何從列AH開始記錄所有這些更改的日期和時間。多個時間戳當一個單元格中的值發生變化

因此,A5中的第一次更改應記錄在AH5列中,A5中的第二次更改應記錄在AI5列中,以此類推。

我發現了多個宏,但他們每次只在同一個單元格中更改日期和時間。

+0

請問您可以發佈您目前使用的宏代碼嗎?這樣我們可以修改現有的代碼來記錄相鄰的單元格。 – Nerdwood

+0

假設用戶已經打開保存的工作簿,那麼應該從'AH5'開始進一步記錄'A5'的變化作爲第一個變化?或者在'AH5:AL5'中開始第一個空單元格? – omegastripes

回答

0

將下面的代碼到目標工作表模塊:

Private Sub Worksheet_Change(ByVal Target As Range) 
    Static RecCell As Range 
    If Target.Address = "$A$5" Then 
     Select Case True 
     Case RecCell Is Nothing 
      Set RecCell = Target.Parent.Range("AH5") 
      RecCell.Value = Target.Value 
     Case RecCell.Column < Target.Parent.Range("AL5").Column 
      Set RecCell = RecCell.Offset(0, 1) 
      RecCell.Value = Target.Value 
     End Select 
    End If 
End Sub 

worksheet change event handler

,在打開的工作簿中的第一A5變化將被保存到AH5,接下來的四個轉變AI5:AL5,進一步修改將被忽略。

UPDATE

這是滿足您的要求,最後的代碼。爲了使它足夠靈活,我添加了一些額外的檢查,禁止記錄該值,如果它不是日期或與先前記錄的相同。您可以通過刪除相應的Case聲明行輕鬆更改這些限制 - 請參閱我的意見。如果e也處理所有更改的單元格。 G。複製的單元格已粘貼到多個選定的單元格。

Private Sub Worksheet_Change(ByVal Target As Range) 
    ' Add reference: Menu - Tools - References - Microsoft Scripting Runtime 
    Static RecList As New Dictionary ' dictionary stores a number of column last record have been made for each row 
    Dim Cell As Range 
    For Each Cell In Target ' loop through all cells that have been changed 
     With Cell 
      Select Case True 
      Case .Column <> 1 ' exit if column is not A 
       Exit Sub 
      Case .Row < 5 Or .Row > 205 ' exit if row is out of target range 
       Exit Sub 
      Case Not IsDate(.Value) ' exit if changed value hasn't got a date format 
       Exit Sub 
      Case Not RecList.Exists(.Row) ' first change in this row 
       RecList(.Row) = Range("AH:AH").Column ' start recording from AH, proceed with value assigning 
      Case .Parent.Cells(.Row, RecList(.Row)) = .Value ' exit if current entered value is the same as the previous 
       Exit Sub 
      Case RecList(.Row) < Range("AL:AL").Column ' the previous populated column is less than AL 
       Set RecList(.Row) = RecList(.Row) + 1 ' continue recording, shift right, proceed with value assigning 
      Case Else ' exit if the previous populated column is AL 
       Exit Sub 
      End Select 
      .Parent.Cells(.Row, RecList(.Row)) = .Value ' assign the value from changed cell 
     End With 
    Next 
End Sub 
+0

謝謝omegastripes。這是訣竅。 – BertusG

+0

Wiil做。問題:如果用戶正在對A5行至A205行進行更改以及對應的第5至205行中的時間戳,我該如何擴展範圍?你上面的代碼僅限於第5行(這是我在Q中要求的)Tx。 – BertusG

+0

@bertusg,需要稍作更改,我會在幾個小時內更新我的答案。也請看看我對你的問題的評論。 – omegastripes

相關問題