Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel: Insert Row when Dates are not Consecutive

Posted on 2012-04-12
6
Medium Priority
?
343 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Technology Partners: 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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

604 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