Consulting

Results 1 to 7 of 7

Thread: Calculating number of days between 2 dates

  1. #1
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location

    Calculating number of days between 2 dates

    Good morning all,

    I am trying to get a routine that will loop through column "L", and if the cell is NOT empty ( it will have a date, mm/dd/yyyy) calculate the difference between that date and a date in column "A" in days, and populate column "M" of the same row with the product.
    Here is the code that I have (that is giving me a type mismatch error).
    Sub Initial_BD_Dates()
        Dim rng As Range
        Dim i As Long
        'Set the range in column L you want to loop through
        Set rng = Range("L3:L175")
        For Each cell In rng
            'test if cell is empty
            If cell.Value <> "" Then
                'write to adjacent cell
                cell.Offset(0, 1).Value = cell.Offset(0, 0).Value - cell.Offset(0, -11).Value
            End If
        Next
    End Sub
    Any ideas? Thanks in advance.

    Chunk
    Last edited by Chunk; 05-19-2017 at 06:42 AM.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't see an issue with your code. Are you using merged cells? Please post a sample workbook.
    Only suggestion, but may not be required
    If cell.Value <> "" And cell.Offset(0, -11) <> "" Then
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    1. You can check to make sure that they really are dates

    2. Attach a small sample workbook that shows the issue will make it easier

    3. An alternative to .Offset that I find easier to read

    Option Explicit
    Sub Initial_BD_Dates()
    
        Dim rng As Range, cl As Range
         
         'Set the range in column L you want to loop through
        Set rng = Range("L3:L175")
        For Each cl In rng.Cells
             With cl.EntireRow
                If Len(.Cells(12).Value) > 0 Then
                    If IsDate(.Cells(12).Value) And IsDate(.Cells(1).Value) Then
                        .Cells(13).Value = .Cells(12).Value - .Cells(1).Value
                    End If
                End If
            End With
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location
    Sample.xlsb


    No merged cells. Here is a sample workbook

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your formatting of A-I is General, not Date. Only Column L which is date formatted, shows the date correctly.

    In a cell below, enter =A3*1 and copy across to verify
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Just 'cuz something looks like a Date, doesn't mean it is

    I added CDate()'s to my first thought above


    Option Explicit
    
    Sub Initial_BD_Dates()
         
        Dim rng As Range, cl As Range
         
         'Set the range in column L you want to loop through
        Set rng = Range("L3:L175")
        For Each cl In rng.Cells
            With cl.EntireRow
                If Len(.Cells(12).Value) > 0 Then
                    If IsDate(.Cells(12).Value) And IsDate(.Cells(1).Value) Then
                        .Cells(13).Value = CDate(.Cells(12).Value) - CDate(.Cells(1).Value)
                    End If
                End If
            End With
        Next
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Feb 2015
    Posts
    81
    Location
    And there it is. After ensuring that the columns were formatted as a date, everything works fine. Thank you for your time and help

Posting Permissions

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