Results 1 to 10 of 10

Thread: excel if statement with a date

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Question excel if statement with a date

    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?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: excel if statement with a date

    I think you can't use an If statement if it's not a number value???

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: excel if statement with a date

    Does VBA support the IsDate Function?

    If so, you can use that.

  4. #4
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel if statement with a date

    Post your code attempt ... that will help us to determine exactly what you are trying to do.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: excel if statement with a date

    Quote Originally Posted by vonoventwin
    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

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel if statement with a date

    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.
    Code:
    '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
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel if statement with a date

    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.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: excel if statement with a date

    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!

  9. #9
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: excel if statement with a date

    =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.
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: excel if statement with a date

    Going off your work I did this:
    VB Code:
    1. Sub Macro1()
    2. If Cells(2, "J").Value = "10/19/2005" Then
    3.     Cells(2, "L").Value = Cells(2, "I").Value * 0.3
    4.     MsgBox "30%"
    5. Else
    6.     Cells(2, "L").Value = Cells(2, "I").Value
    7.     MsgBox "Not 30%"
    8. End If
    9.  
    10. End Sub

    What I want to do now is make this code work for the whole sheet. Basically:
    VB Code:
    1. If Column("J").Value = "10/19/2005" Then
    2. Column("L").Value = Column("I").Value * 0.3
    3.    
    4. Else
    5.     Column("L").Value = Column("I").Value
    6.    
    7. End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width