Click to See Complete Forum and Search --> : excel if statement with a date
vonoventwin
Dec 15th, 2005, 06:48 AM
I have a date in cell A1 (10/19/2005) that I am trying to write an If statement for and it keeps returning it as False when I put in the statement builder A1="10/19/2005" I only have this problem with dates, why?
vonoventwin
Dec 15th, 2005, 07:12 AM
I think you can't use an If statement if it's not a number value???
Hack
Dec 15th, 2005, 07:35 AM
Does VBA support the IsDate Function?
If so, you can use that.
Webtest
Dec 15th, 2005, 07:37 AM
Post your code attempt ... that will help us to determine exactly what you are trying to do.
vonoventwin
Dec 15th, 2005, 07:43 AM
when I put in the statement builder A1="10/19/2005" I only have this problem with dates, why?
didn't write code I used statement builder
Webtest
Dec 15th, 2005, 07:49 AM
I don't know about "Statement Builder", but dates work just fine in VBA code. You just have to consider that the Date/Time data type is a unique animal. It is essentially a floating point number with special handling/formatting/conversion.'A1 10/19/05
'A2 10/22/05
'A3 10/30/05
'A4 10/31/05
'A5 11/2/05
Option Explicit
Sub Macro1()
If Cells(3, "A").Value > Cells(2, "A").Value Then
MsgBox Cells(2, "A").Value
Else
MsgBox Cells(3, "A").Value
End If
If Cells(4, "A").Value > Now() Then
MsgBox Cells(4, "A").Value
Else
MsgBox Now()
End If
End Sub
Webtest
Dec 15th, 2005, 07:53 AM
A date is not a string. As I said, I don't know about "Statement Builder", but try A1 = #10/19/2005#. SQL notation for dates is like this.
vonoventwin
Dec 15th, 2005, 09:13 AM
The date is recognized as 38644 in General format, so I wrote my code as A1=38644 and it sees it as true even though the format is still as a date. Weird.. But it works!
Webtest
Dec 15th, 2005, 09:40 AM
=IF(A1=DATEVALUE("12/10/2005"),"True","False")
DATEVALUE converts the string (if possible) to a Date/Time data type. I hope this helps you. I still don't know what "Statement Builder" is or how you get into it, but it is probably just a wizard for the Formula Editor field.
vonoventwin
Dec 15th, 2005, 09:58 AM
Going off your work I did this:
Sub Macro1()
If Cells(2, "J").Value = "10/19/2005" Then
Cells(2, "L").Value = Cells(2, "I").Value * 0.3
MsgBox "30%"
Else
Cells(2, "L").Value = Cells(2, "I").Value
MsgBox "Not 30%"
End If
End Sub
What I want to do now is make this code work for the whole sheet. Basically:
If Column("J").Value = "10/19/2005" Then
Column("L").Value = Column("I").Value * 0.3
Else
Column("L").Value = Column("I").Value
End If
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.