2016-06-06 34 views
0

我試圖創建一個跟蹤生產計算機停機時間的表。操作員將使用具有列停機時間開始和停機時間的表格。每當有事情發生,他們不得不離開我想讓他們只需要在停機時間統計題目下點擊空單元格,時間就會出現/記錄自己在單元格中,然後同樣停下來。將excel中的VBA時間戳記限制爲只有幾行

我有以下的代碼工作的一點是,如果我在列G和H任意位置單擊當前時間顯示:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

'Check to see if the click/selected cell is in columns A or B 
If Not Intersect(Target, Range("G:H")) Is Nothing Then 

    'Make sure just one cell is selected: 
    If Target.Cells.Count = 1 Then 

     'Update the value 
     Target.Value = Now() 
    End If 
End If 
End Sub 

我如何使它所以時間纔會顯示出來如果點擊一系列的行?現在,如果我點擊列標題,它會改變爲當前時間,我不想要。

感謝您的幫助

+1

如果是這樣的話:'如果Target.Row <= 2 Then Exit Sub'(你必須調整到標題行)。 – Ralph

回答

1

這個怎麼樣?

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    'Check to see if the click/selected cell is in columns A or B 
    If Not Intersect(Target, Range("G:H")) Is Nothing Then 

     'Make sure just one cell is selected: 
     If Target.Cells.Count = 1 And Target.Row > 1 and Target.Row <= SomeNumber Then 

      'Update the value 
      Target.Value = Now() 
     End If 
    End If 
End Sub 
1

我相信你想

If Target.Row > some number and Target.Row < some other number and (Target.Column = 7 or Target.Column = 8) and Target.Cells.Count = 1 then 
    Target.Value = Now() 
End If