Looping through months and years

eran3185

Board Regular
Joined
Apr 28, 2007
Messages
142
Hi

I Want to build an excel file that looks like this:
On column A the user put years (i.e: a1 is 2015 , a2 is 2016 , a3 is 2017)
I want the macro will put on column C the year/month (i.e: c1 is 1/2015 , c2 is 2/2015 , c12 is 12/2015 , c13 is 1/2016 , c36 is 12/2017)
I want to get 36 cells with year/month.

I try to build this macro , but its not work ok.

This is what i have:



Sub loop()

Dim i As Integer, j As Integer, t As Integer


Columns("c:c").Select
Selection.NumberFormat = "@"


t = Application.WorksheetFunction.CountA(Columns(1)) * 12


For j = 1 To 2
For i = 1 To 12
For m = 1 To t

Range("c" & m).Value = i & "/" & Range("a" & j)

Next m
Next i
Next j

End Sub

I need help to do it correct :)
TNX
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I know you asked for a macro, but can you use a formula? If so, put this formula in cell C1 and copy it down to cell C36...

=MOD(ROWS(C$1:C1)-1,12)+1&"/"&INDEX(A$1:A$3,INT((ROWS(C$1:C1)-1)/12)+1)

If you could have a different number of cells in Column A, then change range I highlighted in red above to the range actually containing your list of years (make sure to retain the $ signs as I show them) and, of course, copy the formula down to the row whose number is 12 times the number of cells in that range.
 
Upvote 0
I know you asked for a macro, but can you use a formula? If so, put this formula in cell C1 and copy it down to cell C36...
But if you do need a macro, then give this one a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub MonthsYears()
  Dim Rng As Range
  Set Rng = Range("A1", Cells(Rows.Count, "A").End(xlUp))
  With Rng.Cells(1).Offset(, 2).Resize(12 * Rng.Rows.Count)
    .Formula = "=MOD(ROWS(C$1:C1)-1,12)+1&""/""&INDEX(" & Rng.Address(1, 1) & ",INT((ROWS(C$1:C1)-1)/12)+1)"
    .NumberFormat = "@"
    .Value = .Value
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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