2015-11-03 96 views
-1

我有兩個單元格A1A2。如果A2的值大於零(0),我想要的是A1採用當前日期mm/dd/yyyy。爲此,我使用在A1下式:取當前日期問題

=IF(E2>0;(TODAY())) 

因此,如果在A2的細胞是例如然後A1將11/03/2015。 如果我保存並在第二天重新打開,A1中的值將自動更改爲新日期。這是我不喜歡的,因爲我想用這種方法來跟蹤我每天的開支。因此,如果我在A2中輸入金額,我希望A1給我當前日期並保留它。

那麼我怎樣才能得到當前的日期,而不必擔心每次我重新打開文件他們將改變?

+0

您必須使用VBA。 –

+0

你將不得不使用VBA。正如你注意到的那樣,在任何地方使用'= Today()'都會更新到文件打開的那一天。使用VBA,您可以有一個等待列A更改的宏,然後獲取結果並將值粘貼到其上。嘗試在宏記錄器中做這個(做你的公式,複製和粘貼值),併發回任何想法。 – BruceWayne

+0

或*觀察* A2大於'0'並且在A1 Ctrl +';'中。 – pnuts

回答

1

你在找什麼是一個宏,而不是一個公式。我假設你在COLUMNS而不是ROWS上執行此操作。 EG:A1和B1,而不是A1和A2。

有什麼區別?

宏:可以在事件上觸發一次。

公式:給定一個輸入,它會不斷重新評估結果。

步驟1)按Alt + F11,這將打開VBA中的代碼編輯器。

步驟2)複製和粘貼下面的代碼到您的「片」您的公式駐留英寸

步驟3)保存工作簿作爲一個啓用宏的電子表格。 (XLSM)。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim cell As Range 
    If (Target.Columns.Count = 1 And Target.Column = 2 And Target.Rows.Count = 1) Then 
     'Check every row, currently this will get executed once 
     For Each cell In Target.Cells 
      'Make sure the value is a number 
      If (IsNumeric(cell.Value)) Then 
       'Make sure the value is greater than 0 
       If (cell.Value > 0) Then 
        'Check to make sure the date column is empty, so it only ever gets written once 
        'Note: You can remove the if statement below, if you want it to get re-evaluated every time you make a change 
        If (Target.Worksheet.Cells(cell.row, 1).Value = "") Then 
         'Using the current modified row, set column 1 to Today 
         'Note: Now() is the same as Today in VBA 
         Target.Worksheet.Cells(cell.row, 1).Value = Now() 
        End If 
       End If 
      End If 
     Next cell 
    End If 
End Sub 

注意:此宏,如果您一次修改一行只被執行,你可以改變它通過去除第一的「Target.Rows.Count = 1」的一部分,如果在多個小區上工作「 '聲明。

注2:如果希望在對列2進行更改時重新評估日期,可以刪除最後嵌套的'if'語句。(又名列B,在VBA列中通過數字引用,而不是字母)。注意3:如果你真的需要它通過行來工作,試着用調試器來逐步瞭解這個宏,然後重新構建它。關閉行而不是列。

+0

感謝您的意見。我會讓你知道這個:)謝謝很多。 – Mdermez