Solved

Excel: Insert Row when Dates are not Consecutive

Posted on 2012-04-12
6
336 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

864 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now