2017-05-04 78 views
0

我有一個電子表格的下面的代碼片段:Excel的VBA細胞的條件沒有得到滿足

enter image description here

當我按下昨天按鈕,我想按鈕,找出所有有今天或明天細胞作爲價值並以昨天取代,即=TODAY()-1

一旦解決了這個問題,其他兩個按鈕就變得微不足道了。

這是當前宏觀:

Sub Yesterday_Click() 
    Dim LastRow As Long 
    Dim LastCol As Long 
    Dim CellRange As Range 

    Dim Today As String 
    Dim Tomorrow As String 
    Dim Yesterday As String 

    Today = "=TODAY()" 
    Tomorrow = "=TODAY()+1" 
    Yesterday = "=TODAY()-1" 

    'Find the last non-blank cell in column A(1) 
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row 

    'Find the last non-blank cell in row 1 
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column 

    Set CellRange = Range(Cells(1, 1), Cells(LastRow, LastCol)) 

    For Each cell In CellRange 
     If cell.Value = Today Or cell.Value = Tomorrow Then 
      cell.Value = Yesterday 
     End If 
    Next cell 
End Sub 

如果我添加一個手錶cell.Valuecell.Value結果是2015-05-04,而不是=TODAY()

如何獲得Excel來評估我在單元格中寫入的內容,即=TODAY(),而不是該單元格包含的值作爲公式的結果?

回答

0

我不清楚天氣你想要一個公式在你的手機或價值。

對於公式使用

cell.Formula = "=TODAY()" 

或數值使用

cell.Value = Evaluate("=TODAY()") 
+0

嗨白韞六,我要被替換的公式。然後,Excel本身會將公式評估爲正確的值。 – wickyd

+0

@wickyd要替換公式,請使用上面我的答案中的第一個代碼。 –

+1

嗨,Peh,那正是我在找的,謝謝。 – wickyd

0
Dim Yesterday As String 

Today = "=TODAY()" 
Tomorrow = "=TODAY()+1" 
Yesterday = "=TODAY()-1" 

'Find the last non-blank cell in column A(1) 
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 

'Find the last non-blank cell in row 1 
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column 

Set CellRange = Range(Cells(1, 1), Cells(LastRow, LastCol)) 

For Each cell In CellRange 
    Debug.Print cell.Address 
    Debug.Print cell.Value 
    If cell.Value = Date Or cell.Value = Date + 1 Then 
    cell.Formula = Yesterday 
    End If 
Next cell 

End Sub