+ Reply to Thread
Results 1 to 3 of 3

For and IF statement error

  1. #1
    don
    Guest

    For and IF statement error


    The following subroutine does not work if the Msgbox is set to
    comment. I get a Type mismatch error on the If Datevalue(c) <= date_
    line
    Yet if I take the comment ' off and it is a live line then it works.

    In addition, if I don't use datevalue(c) as part of the If statement ,
    rather just use If c <=date_ then it seems to work without the msgbox
    line.

    Why would I be having this problem???
    Thanks

    Don


    Sub Check_PO_dates()
    Dim date_ As Date
    'Checks for dates and deletes line items that have
    'date = or older then last run
    date_ = Sheets("times").Range("g29")
    Sheets("PO_scratch").Select
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    'MsgBox Cells(2, "f").Row
    For Each c In Range(Cells(2, "F"), Cells(lr, "F"))
    'MsgBox c
    If DateValue(c) <= date_ Then
    Rows(c.Row).ClearContents
    End If
    Next
    End Sub

  2. #2
    Chip Pearson
    Guest

    Re: For and IF statement error

    Don,

    Change

    If DateValue(c) <= date_ Then

    to
    If C.Value <= date_ Then


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com

    "don" <donh7777@excite.com> wrote in message
    news:u0i9v1ldhp3m2s76brho6u1imolujrp0om@4ax.com...
    >
    > The following subroutine does not work if the Msgbox is set to
    > comment. I get a Type mismatch error on the If Datevalue(c) <=
    > date_
    > line
    > Yet if I take the comment ' off and it is a live line then it
    > works.
    >
    > In addition, if I don't use datevalue(c) as part of the If
    > statement ,
    > rather just use If c <=date_ then it seems to work without the
    > msgbox
    > line.
    >
    > Why would I be having this problem???
    > Thanks
    >
    > Don
    >
    >
    > Sub Check_PO_dates()
    > Dim date_ As Date
    > 'Checks for dates and deletes line items that have
    > 'date = or older then last run
    > date_ = Sheets("times").Range("g29")
    > Sheets("PO_scratch").Select
    > lr = Cells(Rows.Count, "A").End(xlUp).Row
    > 'MsgBox Cells(2, "f").Row
    > For Each c In Range(Cells(2, "F"), Cells(lr, "F"))
    > 'MsgBox c
    > If DateValue(c) <= date_ Then
    > Rows(c.Row).ClearContents
    > End If
    > Next
    > End Sub




  3. #3
    Tom Ogilvy
    Guest

    Re: For and IF statement error

    Sub Check_PO_dates()
    Dim date_ As Date
    'Checks for dates and deletes line items that have
    'date = or older then last run
    date_ = Sheets("times").Range("g29")
    Sheets("PO_scratch").Select
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    'MsgBox Cells(2, "f").Row
    For Each c In Range(Cells(2, "F"), Cells(lr, "F"))
    MsgBox c.Text
    if isdate(c) then
    If DateValue(c) <= date_ Then
    Rows(c.Row).ClearContents
    End If
    end if
    Next
    End Sub

    --
    Regards,
    Tom Ogilvy


    "don" <donh7777@excite.com> wrote in message
    news:u0i9v1ldhp3m2s76brho6u1imolujrp0om@4ax.com...
    >
    > The following subroutine does not work if the Msgbox is set to
    > comment. I get a Type mismatch error on the If Datevalue(c) <= date_
    > line
    > Yet if I take the comment ' off and it is a live line then it works.
    >
    > In addition, if I don't use datevalue(c) as part of the If statement ,
    > rather just use If c <=date_ then it seems to work without the msgbox
    > line.
    >
    > Why would I be having this problem???
    > Thanks
    >
    > Don
    >
    >
    > Sub Check_PO_dates()
    > Dim date_ As Date
    > 'Checks for dates and deletes line items that have
    > 'date = or older then last run
    > date_ = Sheets("times").Range("g29")
    > Sheets("PO_scratch").Select
    > lr = Cells(Rows.Count, "A").End(xlUp).Row
    > 'MsgBox Cells(2, "f").Row
    > For Each c In Range(Cells(2, "F"), Cells(lr, "F"))
    > 'MsgBox c
    > If DateValue(c) <= date_ Then
    > Rows(c.Row).ClearContents
    > End If
    > Next
    > End Sub




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1