簡體   English   中英

如何使用VBA獲取Excel電子表格以自動填充日期和時間?

[英]How to get Excel Spreadsheet to auto populate date & time using VBA?

嘗試在Excel上獲取啟用宏的工作表,以便在B或C列中輸入任何值時自動填充日期和時間。

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Integer
  For i = 2 To 100
    If Cells(i, "B").Value <> " " And Cells(i, "C").Value = " " Then
      Cells(i, "F").Value = Date & " " & Time
      Cells(i, "F").NumberFormat = "m/d/yyyy h:mm AM/PM"
    End If
  Next
  Range("F:F").EntireColumn.AutoFit
End Sub

我編寫的代碼有什么問題嗎?

“目標”將是已更改的單元格。 一次可以更改一個以上的單元格(通過ctrl輸入),因此檢查Target中的所有單元格不是一個壞主意。

如果使用“相交”方法,它將僅獲得“目標”區域和要檢查的重疊區域。 然后,它將循環遍歷那些單元格(如果有的話),並且如果找到一個值,則對它們進行時間戳記。

正如其他人提到的那樣,在插入圖章之前禁用事件將阻止調用另一個工作表更改事件。 調試時要小心,不要遺漏事件。

您可以在此處閱讀有關事件參數的更多信息: https : //msdn.microsoft.com/zh-cn/library/office/ff839775.aspx

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Excel.Range
    Dim cll As Excel.Range
    Set rng = Excel.Intersect(Target, Range("B:C"))
    If Not (rng Is Nothing) Then
        Excel.Application.EnableEvents = False
        For Each cll In rng.Cells
            If Len(cll.Formula) > 0 Then
                Cells(cll.Row, 6).Value = Format$(Now, "m/d/yyyy h:mm AM/PM")
            End If
        Next
        Range("F:F").EntireColumn.AutoFit
        Excel.Application.EnableEvents = True
    End If
End Sub

您不想每次工作表上的任何更改都經歷所有這些; 僅當影響時間戳有效性的內容發生變化時。 通常,我們將使用“ Intersect來確定更改后的值之一是否應接收新的時間戳。 您也不想讓例程嘗試在自身之上運行,因此建議在更改值(即添加時間戳)之前關閉事件處理。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B:C")) Is Nothing Then
        On Error GoTo SafeExit
        Application.EnableEvents = False
        Dim bc As Range   'no sense in declaring something until we actually need it
        For Each bc In Intersect(Target, Range("B:C")) 'deal with every cell that intersects. This is how to handle pastes into more than one cell
            If Not IsEmpty(Cells(bc.Row, "B")) And Not IsEmpty(Cells(bc.Row, "C")) Then
                Cells(bc.Row, "F").Value = Now 'Now is the equivalent of Date + Time
                Cells(bc.Row, "F").NumberFormat = "m/d/yyyy h:mm AM/PM"
            End If
        Next bc
        'Range("F:F").EntireColumn.AutoFit 'this slows things down. you may want to comment this out and just set an apprpriate column width that will handle everything
    End If
SafeExit:
    Application.EnableEvents = True
End Sub

這就是我對這個老問題的看法。 有很多例子。 在此頁面右側的“ 相關”部分中查找一些鏈接。

幾個小變化:

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim i As Integer
  Application.EnableEvents = False
    If Target.Column = 2 Or Target.Column = 3 Then
      For i = 2 To 100
        If Cells(i, "B").Value <> " " And Cells(i, "C").Value = " " Then
          Cells(i, "F").Value = Date & " " & Time
          Cells(i, "F").NumberFormat = "m/d/yyyy h:mm AM/PM"
        End If
      Next
    End If
  Range("F:F").EntireColumn.AutoFit
  Application.EnableEvents = True
End Sub

關閉偶數,這樣當您對代碼進行修改時就不會觸發它,並測試目標列以查看它是B還是C,並且只有在它出現時才觸發

另外,您知道您的代碼將更新第2行到第100行,而不管哪一行更改正確嗎? 如果只希望更改的行,可以通過target.row獲得

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM