Insert Missing Date VBA Question

Netsurfr

New Member
Joined
Sep 4, 2008
Messages
6
This VBA code inserts missing dates in column A which works when the month does not start with the 1st. When the month does start with the first I get the following duplication. What am I missing to prevent this? Thank you in advance for your help.

1587150903561.png


Sub Insert_Days()
Dim r As Long, d As Date
r = 2 'start row
EOM = DateSerial(Year(Range("A" & r)), Month(Range("A" & r)) + 1, 0) 'End of month
d = DateSerial(Year(Range("A" & r)), Month(Range("A" & r)), 1) 'Beginning of month
Application.ScreenUpdating = False
Do While d <= EOM
DoEvents
If Range("A" & r) > d Then
Rows(r).Insert
Range("A" & r).Value = d
d = d + 1
ElseIf Range("A" & r) = "" Then
Range("A" & r).Value = d
d = d + 1
End If
r = r + 1
If Range("A" & r) = d Then d = d + 1
Loop
Application.ScreenUpdating = True
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try flipping the order of these two lines:
VBA Code:
r = r + 1
If Range("A" & r) = d Then d = d + 1
so its:
VBA Code:
If Range("A" & r) = d Then d = d + 1
r = r + 1
 
Upvote 0
Try flipping the order of these two lines:
VBA Code:
r = r + 1
If Range("A" & r) = d Then d = d + 1
so its:
VBA Code:
If Range("A" & r) = d Then d = d + 1
r = r + 1

Yes, it works perfectly now! Thank you very much it was causing me so much distress.
 
Upvote 0

Forum statistics

Threads
1,214,825
Messages
6,121,787
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top