簡體   English   中英

當另一個單元格中有值輸入時,Excel在單元格中輸入日期時間

[英]Excel entering the date time into a cell when another cell has a value entered into it

我試圖通過在最后一列的數據輸入后添加Now()值來創建一列,作為每一行的唯一鍵值。 我創建了一個子程序,該子程序檢查要選擇的一個單元格的范圍並在其中包含一個值,如果是,則將Now()值添加到另一列以充當該行的唯一標識符。 這是我到目前為止的代碼:

Private Sub Worksheet_SelectionChange(ByVal Target As range)

    'Check to see if the click/selected cell is in column H
    If Not Intersect(Target, range("H:H")) Is Nothing Then

        'Make sure just one cell is selected and it isnt empty
        If Target.Cells.Value <> "" And Target.Row > 7 And Target.Row <= 20 Then

            'Update the "KeyA" value
            range("A" & Target.Row).Value = Now()
        End If

    End If

End Sub

不幸的是,如果所選單元格中已經有一個值,這只會將Now()值添加到鍵列中,因此,如果我單擊一個空單元格然后添加該值,則不會將Now()添加到鍵列中。 將條目添加到所選單元格后,如何使子項添加Now()值,而不必在向其添加值之后重新選擇該單元格?

使用Worksheet_Change事件而不是Worksheet_SelectionChange

Change目標單元格中​​的值時,將觸發Change ,而SelectionChange其他單元格時,將觸發SelectionChange

暫無
暫無

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

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