2017-05-04 2 views
0

I 스프레드 시트의 다음 코드가 : 나는 어제 버튼을 누르면엑셀 VBA 셀 조건이 충족되지

enter image description here

, 내가 원하는 버튼을 오늘이나 내일 중 하나가 모든 셀을 찾을 수를 를 값으로 사용하고 어제 (즉, =TODAY()-1)로 바꿉니다.

이 문제가 해결되면 다른 두 개의 버튼이 쉽게 해결됩니다.

가 여기에 현재 매크로입니다 : 내가 cell.Value에 시계를 추가하는 경우

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.Value의 결과는 대신 =TODAY()의, 2015-05-04입니다.

수식의 결과로 셀에 포함 된 값 대신 Excel에 내가 쓴 내용, 즉 =TODAY()을 평가하도록하려면 어떻게합니까?

답변

0

셀 또는 수식의 공식을 기쁘게하는 것은 확실하지 않습니다. 공식에 대한

cell.Formula = "=TODAY()" 

또는 값이

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

안녕 PEH를 사용하여 사용, 나는 수식을 교체해야합니다. 그런 다음 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