Solved

Excel: Insert Row when Dates are not Consecutive

Posted on 2012-04-12
6
341 Views
Last Modified: 2012-04-14
Hi,

In the attached file you could see what I have as raw data and in the other sheet the result as i Need it. (There is not need to higlight the inserted rows)

Basically I have some data which in column "A" are dates (dd/mm/yyyy).
The other columns are names and numbers.

What I need is a macro that detects when the dates (between rows) in column "A" are not consecutive and insert a row for each missing day...

I.E.
If "A4" is 03/02/2012 and "A5" is 05/02/2012 then I need to insert a row between both rows and in column "A" there must show the missing Date and in column "C" there should say: NO SALE.

If the difference between rows are more than one single day then I need to insert a row for each missing day.

Could you help me please ?

Thank you in advance for your time.
Roberto.
SAMPLE-DATE-INSERT.xls
0
Comment
Question by:Pabilio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37839598
Sub insunuseddates()
    Dim cel As Range, nr As Long, i As Long
    For i = Range("A4").End(xlDown).Row To 4 Step -1
        Set cel = Cells(i, 1)
        nr = Int(cel) - Int(cel.Offset(-1))
        If nr > 1 Then
            cel.Select
            cel.Resize(nr - 1).EntireRow.Insert
            cel.Offset(-nr + 1).Resize(nr - 1).FormulaR1C1 = "=r[-1]c+1"
            cel.Offset(-nr + 1).Resize(nr - 1).Value = cel.Resize(nr - 1).Value
            cel.Offset(-nr + 1, 3).Resize(nr - 1).Value = "NO SALE"
        End If
    Next i
End Sub
0
 
LVL 5

Author Comment

by:Pabilio
ID: 37839870
Hi Ssaqibh,

The code inserts the rows perfectly...
The only problem is with the dates in the inserted rows...
It has the same date than the biggest date instead of the consecutive day.

I believe the problem is in this line:
cel.Offset(-nr + 1).Resize(nr - 1).Value = cel.Resize(nr - 1).Value
 But I'm not able to correct it.

I really appreciatte your help.

Regards
Roberto.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 37839908
Sorry

            cel.Offset(-nr + 1).Resize(nr - 1).Value = cel.Offset(-nr + 1).Resize(nr - 1).Value
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 5

Author Comment

by:Pabilio
ID: 37840089
Ssaqibh,

I'm having problems with the format in the inserted rows...
It should be dd/mm/yyyy but instead is doing mm/dd/yyyy
I'm triying numberformat but without success.
Could you help me please '

Regards,
Roberto.
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 37840654
See if this works

Sub insunuseddates()
    Dim cel As Range, nr As Long, i As Long
    For i = Range("A4").End(xlDown).Row To 4 Step -1
        Set cel = Cells(i, 1)
        nr = Int(cel) - Int(cel.Offset(-1))
        If nr > 1 Then
            cel.Select
            cel.Resize(nr - 1).EntireRow.Insert
            cel.Offset(-nr + 1).Resize(nr - 1).FormulaR1C1 = "=r[-1]c+1"
            cel.Offset(-nr + 1).Resize(nr - 1).NumberFormat = "dd/mm/yy"
            cel.Offset(-nr + 1).Resize(nr - 1).Value = cel.Offset(-nr + 1).Resize(nr - 1).Value
            cel.Offset(-nr + 1, 3).Resize(nr - 1).Value = "NO SALE"
        End If
    Next i
End Sub
0
 
LVL 5

Author Closing Comment

by:Pabilio
ID: 37847068
Hi Ssaquibh,

It works perfectly !!...

I really appreciate it.
Thanks a lot for the code.

Roberto.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question