lundi 9 mars 2020

How can I make Today()-365 or now()-365 a variable that is used in an IF statement for the logic test?

I have a table in excel that I have created using vba and I've created a column called Date_Check. The algorithm of the formula is basically if a fail date is before Today()-365 which is a year ago from now, put the word "bad" in that column else put " Good". The code works on the entire range in the specific column as shown below:

Dim LastYear  as Date
'LastYear is the date a year ago  time the code is run 
LastYear = Today() -365

'Date_Check is a column in my table(BigTable)
Range("BigTable[Date_Check]").Formula = "= IF((BigTable[FAILDATE]<  LastYear ), ""Bad"", ""Good"")"

I tried to run this code but it gave me the #Name? error on excel. When I troubleshot the code, it showed that the variable LastYear is not being recognized or able to compare with the dates in the Fail date column?

I know putting the Today() -365 directly in the if statement resolves the issue but it makes the code extremely slow:

Range("BigTable[Date_Check]").Formula = "= IF((BigTable[FAILDATE]<  (Today() -365)), ""Bad"", ""Good"")"

how can I get the LastYear variable to be recognized which will make the code run faster?

Aucun commentaire:

Enregistrer un commentaire