Solved

Excel: Insert Row when Dates are not Consecutive

Posted on 2012-04-12
6
335 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 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

11 Experts available now in Live!

Get 1:1 Help Now