简体   繁体   中英

excel vba formula with date compare in IF function within a formula

Let's say cell A1 is a Date: 23/06/2017

Now in excel if I put the following formula in cell B1:

=  IF(INT(C2)>INT(2/15/2017);"Year1";"Year2") 

then it's working fine.

However, If I try to put it thought VBA coda as:-

Set sht1 = xWb.Sheets("ProcessedData")
sht1.Range("B1").Select
sht1.Range("B1").Formula = _
    "=IF(INT(A1)>INT(2/15/2017);""Year1"";""Year2"")" 

It is giving me error 1004 (Object not found). I tried with CDate but I think, I am missing something here.

Can you please help me with this issue.

Excel is English oriented , you are using ; in your formula.

So either change your ; to , inside your formula string, like:

sht2.Range("B1").Formula = "=IF(INT(A1)>INT(2/15/2017),""Year1"",""Year2"")"

Or, change Formula to FormulaLocal as in the following line:

sht2.Range("B1").FormulaLocal = "=IF(INT(A1)>INT(2/15/2017);""Year1"";""Year2"")"

Note : in the Formula line I think you meant to use sht1 and not sht2 , so it should be:

sht1.Range("B1").Formula = "=IF(INT(A1)>INT(2/15/2017),""Year1"",""Year2"")"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM