vb manipulation of date data on sheet

asylum

Board Regular
Joined
Dec 2, 2003
Messages
243
Hi, I have a sheet with a string of date, 365 lines long as below..

03/01/2016 Sunday 1
04/01/2016 Monday 2
05/01/2016 Tuesday 3
.
.
.

I am trying to write a bit of code to manipulate the sheet so that it inserts a line at the start of every month, and in column A of that row it puts the Name (in text) of the month following, so i end up with:

January
03/01/2016 Sunday 1
.
.

etc

so far i have:

Sub monthdef()

Dim month As Variant
Dim R As Range

month = Range("a2").Value

MsgBox month

For Each R In Range("c2:c367")

If R.Value = 1 Then

rows(R.Row).Select
Selection.Insert shift = xlDown
Range("a" & R.Row - 1).Select
ActiveCell.Value = month

Range("c" & R.Row + 15).Select

month = month + 32

MsgBox month

End If

Next R

End Sub

but this cant 'jump' the first 1 it comes to! and I don't know the code to get the text value of month into the row eiether.

Can you help?

Thanks

Andy
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Is your list of dates every day in the month or just the first of the months? Also, you have January appear right before 3/1/2016. How do you determine what month to display?
 
Upvote 0
hi, yes it is a list of every day in the month, but it may be a different month at the start of the list every time.

the dtae shown above is an unfortunate typo, they should all start with 01/01, howevr the determination of when a month ends and the line being inserted is done via the 1,2,3 etc in column c, which is the day of the month, when this is a 1 i insert a line above.

Thanks
 
Upvote 0
Try
Code:
Sub monthdef()
  Dim R
  For R = 367 To 2 Step -1
    If Cells(R, 3) = 1 Then
      Cells(R, 3).EntireRow.Insert shift:=xlDown
      Cells(R, 1) = MonthName(Month(Cells(R + 1, 1)))
    End If
  Next R
End Sub
 
Upvote 0
Hi Warship, thats great thanks, I had just come to the realisation that I would have to work UP the list and not down, great solution, many thanks for your help!

Andy
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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