+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Macro to change date format and then run function based on greater than or equal to

    Hi there

    What I want is for the date format to be changed from dd/mm/yy 00:00:00 to dd/mm/yy which I can do with formula
    Code:
    =INT(H6)
    but I'm not sure how to do this in code form column H

    I then need to take the new date format and if that is greater than or equal to 20/01/2010 then run the formula
    Code:
    =H1+67 - Weekday(H1+61)
    All in a macro.

    I was trying advanced filter but I am not sure how to do this.

    This code works for the full column but does not only run weekday formula for anything after 20/01/2010 plus is in wrong date format so cannot do analysis on it after.

    Code:
    Private Sub DetermineDocsDeadline(m, Rtn)
    If ws1.Cells(m, "H") >= "19/01/2010 00:00:00" Then
    Rtn = ws1.Cells(m, "H") + 67 - Application.WorksheetFunction.Weekday(ws1.Cells(m, "H") + 61)
    End If
    End Sub
    All help gratefull recieved.


    Libby

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,498

    Re: Macro to change date format and then run function based on greater than or equal

    Hello Libby,

    It looks like you need a User Defined Function or UDF for short. A UDF can be used on a Worksheet like a formula. If I understand what you want to do correctly then this UDF should do it. If not, let me know and we'll work on it. The result is returned to the cell that called the UDF.
    Code:
    Function DetermineDocsDeadline(m As Range) As Variant
    
      Dim D As Long
        
        If IsDate(m) = True Then
           D = CLng(m)
           If D >= CLng(CDate("19/01/2010")) Then
             DetermineDocsDeadline = CDate((D + 67) - (Weekday(D) + 61))
           End If
        End If
        
    End Function
    UDF Example
    Code:
       'Place this code in a cell
       =DetermineDocsDeadline(H1)
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Re: Macro to change date format and then run function based on greater than or equal

    Hi Leith,

    Thanks for this. I was trying to avoid putting any formula in the sheet as the sheet has around 30 thousand rows and it adds to the memory.

    Plus it won't let me put
    Code:
    =DetermineDocsDeadline(H1)
    in it says name not valid.

    Is there any way we can put it in the code without having any formula in the sheet. If not what do I need to do to allow this to work.

    Thank you and apologies for the delay in getting back to you.

    Libby

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,498

    Re: Macro to change date format and then run function based on greater than or equal

    Hello Libby,

    I have rewritten the macro. You can either run this using ALT+F8 (Macro Dialog) or by attaching it to a button on the sheet. It assumes the data is on the "Sheet1". It compares the values in column "H" against the deadline in the macro. Once all the dates have been checked, the new dates are copied over to "Sheet2" starting in cell "A1".
    Code:
    'Written: March 18, 2010
    'Author:  Leith Ross
    
    Sub DetermineDeadlines()
    
      Dim D As Long
      Dim Deadline As Long
      Dim DocDates() As Variant
      Dim DstWks As Worksheet
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
        
        Deadline = CLng(CDate("19/01/2010"))
        
        Set SrcWks = Worksheets("Sheet1")
        Set DstWks = Worksheets("Sheet2")
        
        Set Rng = SrcWks.Range("H1")
        Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, SrcWks.Range(Rng, RngEnd))
        
        ReDim DocDates(1 To Rng.Rows.Count, 1 To 1)
        DocDates = Rng.Value
        
          For Each Item In DocDates
            If IsDate(Item) = True Then
              D = CLng(Item)
              If D >= Deadline Then
                Item = CDate((D + 67) - (Weekday(D) + 61))
              End If
            End If
          Next Item
          
          DstWks.Cells.ClearContents
        
          Set Rng = DstWks.Range("A1").Resize(UBound(DocDates), 1)
          Rng.Value = DocDates()
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    01-21-2010
    Location
    Glasgow
    MS-Off Ver
    Excel 2003 and 2007
    Posts
    105

    Re: Macro to change date format and then run function based on greater than or equal

    Hi there,

    Thank you for this. Unfortunately this coopies and pastes everything from 20/01/2010 into A1 of Sheet 2 but that is it. It is not removing the time facttor or adding my formula.

    I suspect it is something I have done. I would like it to return the item in column I if poss too.

    I'm not sure if it is me but I suspect it is.

    Thank you
    Libby

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.2.0