+ Reply to Thread
Results 1 to 17 of 17

Fill missing dates in a sequence from the 1st of the month until the last day of the month

  1. #1
    Registered User
    Join Date
    04-04-2013
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    6

    Post Fill missing dates in a sequence from the 1st of the month until the last day of the month

    Hi,

    I am have a spread sheet that has dates in "B" and ranges to "F" with other data elements. The data provided in the Date column only accounts for workdays, however I need to have the all the dates of the month that I am working on. I am working on a code that works fine until I get towards the end of the month (e.g. the last date provided is 03/28/13 but I also need the code to add in dates after the last date provided meaning 03/29/13, 03/30/13, 03/31/13. This is the code that I have so far:

    Sub ABC()
    Dim rw As Long, colDate As String, colDay As String
    Dim colLocation As String
    rw = 2
    colDate = "B"
    Do
    If Cells(rw + 1, colDate) = "" Or Cells(rw, colDate) = "" Then
    rw = rw + 1
    Else
    If Cells(rw, colDate) < Cells(rw + 1, colDate).Value - 1 Then
    Rows(rw + 1).EntireRow.Insert
    Cells(rw + 1, colDate).Value = Cells(rw, colDate) + 1
    rw = rw + 1
    Else
    rw = rw + 1
    End If
    End If
    If rw > Cells(Rows.Count, colDate).End(xlUp).Row Then Exit Do
    Loop
    End Sub

    How can I make it complete the task until the end of the month insted of just until the last day provided by the sheet? Can someone help me out with this?

    Best

    Kaygu

  2. #2
    Registered User
    Join Date
    04-04-2013
    Location
    Here
    MS-Off Ver
    Excel 2010/2013
    Posts
    8

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Hi,

    To determine the last day of the month, use:

    Please Login or Register  to view this content.
    Source code : http://www.excel-pratique.com/en/vba...in_a_month.php

    V-B-A

  3. #3
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    It also should be possible to use the Excel function "EoMonth" i.e.
    Please Login or Register  to view this content.
    Alf

  4. #4
    Registered User
    Join Date
    04-04-2013
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Hi Alf,

    Thank you for your reply. How would I add your code into mine so i would get the desired result. This is my first time writing in VBA and I have no experience. Could you help me out to get the complete code together?

    Thanks

    Kaygu

  5. #5
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Perhaps like this?
    Please Login or Register  to view this content.
    Is startdate found in B2? If so replace startdate with
    Please Login or Register  to view this content.
    Alf

  6. #6
    Registered User
    Join Date
    04-04-2013
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Thanks for explaining further. I am just having a bit of trouble on where within my code to put your EoMonth function or what code to replace with it. My code now looks like this but it does not work. I get "compile error: Else without If" error message when trying to run the macro.

    Sub ABC()
    Dim rw As Long, colDate As String, colDay As String
    Dim colLocation As String
    rw = 2
    colDate = "B"
    Do
    If Cells(rw + 1, colDate) = "" Or Cells(rw, colDate) = "" Then
    rw = rw + 1
    Do Until Cells(rw, colDate) = WorksheetFunction.EoMonth("B2", 0)
    Else
    If Cells(rw, colDate) < Cells(rw + 1, colDate).Value - 1 Then
    Rows(rw + 1).EntireRow.insert
    Cells(rw + 1, colDate).Value = Cells(rw, colDate) + 1
    rw = rw + 1
    Else
    rw = rw + 1
    End If
    End If
    If rw > Cells(Rows.Count, colDate).End(xlUp).Row Then Exit Do
    Loop
    End Sub

    kaygu

  7. #7
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    I would try

    Please Login or Register  to view this content.
    and block line

    Please Login or Register  to view this content.
    If this does not work perhaps you could upload the wookbook?

    Alf

  8. #8
    Registered User
    Join Date
    04-04-2013
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Hi,

    I can't get it to work. I have attached a workbook with a sheet for before and how it should look after the macro has run. Hope this gives you more insight.

    Thanks
    Attached Files Attached Files

  9. #9
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Ok here we go.

    Run macro "AddDate" and see if this is what you wanted.

    Alf
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    04-04-2013
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Thank you very much the macro works great. I have one last thing. I need to macro to perform the same task for multiple date ranges within the column until all of the dates are done (e.g. in my orginial file i have up to 50 times the same date under different stocks. I sort the data for stocks and then date, so that the dates are in order for each indiviual stock, then I would run your macro to fill in the missing dates. So the macro needs to do the task over again until all date gaps are filled). Would that be possible? That would be the biggest time saver for me.

    Thanks

    Kaygu

  11. #11
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    so that the dates are in order for each indiviual stock, then I would run your macro to fill in the missing dates
    I think that should be possible but could you upload another sample file where I can see the layout in order to modify the macro?

    Then I wonder, at the moment this macro starts by checking the last date and add the end of the month date if this is missing. How about the start date i.e. the 1st of the month? This could just as easily being checked and added if missing.

    Alf

  12. #12
    Registered User
    Join Date
    04-04-2013
    Location
    Boston
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Thanks Alf.

    I have attached another sample file. The dates that are missing for each stock can be different dates and in this sample file I just show for example 2 stocks there would be more coming after those. The starting date is always the first of the month. I think I basically just need the macro to loop after the first stock or date range is completed.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    11-20-2019
    Location
    Albuquerque, NM
    MS-Off Ver
    2010
    Posts
    3

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Hello All,
    I really appreciate all the help in this forum. Saves me a bunch of time and i have been able to learn a lot.

    I need some help, I am working on the same thing Kaygu was doing but my data has some duplicate dates. I used the macro Alf put together and it works great, but I think my duplicate dates prompt an error. Can I get some help?

    Thanks
    Timothy

  14. #14
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Hi Timothy

    Welcome to this forum nice to have you here. One problem with you request for help is that you posted it in an existing thread and this is a no no as forum rule 4 states that.

    Do not post a new help request in an existing thread
    So you need to start you own thread. You may of course add a link to this thread if you think it would be useful. There is also the advantage when starting a new thread that you have a much better chance of getting help.

    So add a descriptive title for you problem and upload a file as well showing before and after results and I'm certain somebody will help you.

    Alf

  15. #15
    Registered User
    Join Date
    11-20-2019
    Location
    Albuquerque, NM
    MS-Off Ver
    2010
    Posts
    3

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Hey Alf,
    Thanks for the info. I will do that.

  16. #16
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    Good to hear, I'll do a search using your forum name to find you new thread and see if I may have a possible suggestion on how to solve your problem.

    Alf

  17. #17
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,758

    Re: Fill missing dates in a sequence from the 1st of the month until the last day of the m

    This is an old thread from April 2013 and macro in now updated as it can not cope with a start date different from the 1st in a month.

    Macro starts by checking if cell B2 (first date in the list) contains a date that corresponds to the first of the month. If not a row is inserted and formate from the inserted row is copied from the row below. Then EoMonth function is used to set the date for the 1st month. Then the last date in the B column is also set by using the same function if missing.

    After that all dates between first and last date in that particular month are checked and if any is missing a new row is insert and the missing date added.

    Please Login or Register  to view this content.
    In the original thread from the OP there should also be a numerical value added in columns A, C, D, E and F but this part of the macro is deleted as I assume they are of no interest.

    Alf
    Last edited by Alf; 11-23-2019 at 05:29 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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