2017-08-08 48 views
1

我有一個跟蹤器,用戶每天都會爲其添加行。 我需要一個顯示當前日期和用戶名的代碼,一旦單元被雙擊。合併列A將是日期單元格,列P將是用戶名。日期和用戶名(如果點擊)

我有一些代碼,但它顯示用戶和日期一旦打勾完成。現在我需要點擊。

Sub CheckBox_Date_Stamp() 

Dim cbx As CheckBox 

'Application.Caller returns the name of the CheckBox that called this macro 
Set cbx = ActiveSheet.CheckBoxes(Application.Caller) 

'.TopLeftCell returns the cell address located at the top left corner of the cbx checkbox 
With cbx.TopLeftCell 
    'Check the checkbox status (checked or unchecked) 
    If cbx.Value = xlOn Then 
     ' Checkbox is Checked 
     ' User Name and Date-Time 
     .Offset(0, 1).Value = Environ("UserName") 
     .Offset(0, 2).Value = Now 
    Else 
     ' Checkbox is unchecked; Clear cell contents? 
     .Offset(0, 1).ClearContents 
     .Offset(0, 2).ClearContents 
    End If 
End With 

End Sub 

你能提供幫助嗎? 謝謝

回答

1

非常難以得到你的意思,但一般來說,你需要在工作表中的事件SelectionChange。添加以下在這裏工作表:

enter image description here

Option Explicit 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

    If Target.Cells.Count <> 1 Then Exit Sub 

    If Target.Address = Cells(1, 2).Address Then 
     Cells(1, 1) = Application.UserName 
     Cells(1, 16) = Now 
    Else 
     Debug.Print "This was not B1" 
    End If 

End Sub 

一旦你點擊了B1你會得到列APUserName和時間。

+0

這是可能改變這個,使得它在我點擊A1而不是B1時工作嗎?將單元格更改爲(1,1)是否工作? – Adam

+0

@Adam - 它可以工作,但你必須改變它在代碼中顯示用戶名的單元格。 – Vityata

+0

好的,難道這不可能在單擊單元格中顯示它嗎?我的意思是我點擊A1 - 日期在A1中彈出? – Adam

0

這將放置在列A/B計算機的用戶名和日期/時間在哪個小區的雙擊。

的代碼添加到工作表,而不是一個獨立的模塊。
您可能還想添加代碼來檢查列A:B是否已經包含數據,或要求確認。

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 
    Cells(Target.Row, 1) = Environ("username") 
    Cells(Target.Row, 2) = Now 
End Sub 
相關問題