Solved

Excel: Insert Row when Dates are not Consecutive

Posted on 2012-04-12
6
340 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

749 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